匿名函数声明的 Scala 类型推断
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Jörg W Mittag 所说,这里绝对没有进行类型推断。
只是一个抽象方法声明(抽象是因为它没有主体)。它可以通过不同的方式实现,具体取决于
Expression
的定义。鉴于您发布的内容,它不能。
There is absolutely no type inference going on here, as Jörg W Mittag says.
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
.It can't, given just what you posted.
我认为关键在于,函数
eval
返回一个函数,其类型为Function2[Expression, Boolean]
。更清楚地说:
绑定
和表达式
之间没有直接关系。I think the key point is that, function
eval
returns a function, whose type isFunction2[Expression, Boolean]
.It's more clear to say:
There is no direct relationship between
binding
andExpression
.