匿名函数声明的 Scala 类型推断

发布于 2024-09-19 22:56:07 字数 317 浏览 3 评论 0原文

我是 Scala 的初学者,我只是好奇 Scala 如何处理此代码片段的类型推断

trait Expression { .... }

def eval (binding : String => Boolean) : Expression => Boolean

我了解绑定是一个将 String 转换为 Boolean 的函数,但是为什么绑定可以同时被声明为表达式的成员呢?它是隐式转换的吗?它是如何运作的?

抱歉,如果我的问题有点令人困惑,

非常感谢:D

I'm a beginner in Scala and I'm just curious about how Scala handles the type inference for this code snippet

trait Expression { .... }

def eval (binding : String => Boolean) : Expression => Boolean

I understand that binding is a function that converts String to Boolean, but why binding at the same time could be declared as a member of Expression? is it implicitly converted? How does it work?

Sorry if my question is a bit confusing

Thanks so much :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦明 2024-09-26 22:56:07

正如 Jörg W Mittag 所说,这里绝对没有进行类型推断。

def eval (binding : String => Boolean) : Expression => Boolean

只是一个抽象方法声明(抽象是因为它没有主体)。它可以通过不同的方式实现,具体取决于Expression 的定义。

为什么绑定可以同时声明为Expression的成员

鉴于您发布的内容,它不能。

There is absolutely no type inference going on here, as Jörg W Mittag says.

def eval (binding : String => Boolean) : Expression => Boolean

is simply an abstract method declaration (abstract because it doesn't have a body). It can be implemented in different ways, depending on the definition of Expression.

why binding at the same time could be declared as a member of Expression

It can't, given just what you posted.

心凉怎暖 2024-09-26 22:56:07

我认为关键在于,函数eval返回一个函数,其类型为Function2[Expression, Boolean]

更清楚地说:

def eval (binding : String => Boolean) : (Expression => Boolean)

绑定表达式之间没有直接关系。

I think the key point is that, function eval returns a function, whose type is Function2[Expression, Boolean].

It's more clear to say:

def eval (binding : String => Boolean) : (Expression => Boolean)

There is no direct relationship between binding and Expression.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文