Scala 中是否有用于在匿名函数内绑定值的语法糖?
我不想写,而是
((x: Double) => (((y: Double) => y*y))(x+x))(3)
想写一些类似
((x: Double) => let y=x+x in y*y)(3)
Scala 中是否有类似语法糖的东西?
Instead of writing
((x: Double) => (((y: Double) => y*y))(x+x))(3)
I would like to write something like
((x: Double) => let y=x+x in y*y)(3)
Is there anything like this sort of syntactic sugar in Scala?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确实有:它被称为“
val
”。 :-)这里的大括号当然是可选的,我只是在定义函数时更喜欢它们而不是括号(毕竟,这不是 Lisp)。
val
关键字定义当前词法范围内的新绑定。 与 Lisp 和 ML 等语言不同,Scala 并不强迫本地人定义自己的范围。实际上,
var
也可以在任何范围内工作,但使用它被认为是不好的风格。Indeed there is: it's called "
val
". :-)The braces are of course optional here, I just prefer them to parentheses when defining functions (after all, this isn't Lisp). The
val
keyword defines a new binding within the current lexical scope. Scala doesn't force locals to define their own scope, unlike languages such as Lisp and ML.Actually,
var
also works within any scope, but it's considered bad style to use it.好的,这是带有装订的内衬:
干杯
OK, here's the one liner WITH the binding:
Cheers