为什么 Scala 和 C# lambda 是双箭头?
有谁知道一些背景知识,为什么 Scala 和 C# 对 lambda 使用双箭头 (=>) 而不是单箭头 (->)?
单箭头在文献中更常见,用于 Haskell、O'Caml、F#、Groovy 等。在我看来,它看起来也更好:)
Does anyone know some background, why Scala and C# use double arrow (=>) for lambdas instead of single arrow (->)?
Single arrow is more common in literature, is used in Haskell, O'Caml, F#, Groovy etc. and IMO it also looks nicer :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 C# 中,
可能是因为在不安全代码的情况下已经使用了
->
运算符 (链接)In C#,
maybe because
->
operator is already used in case of unsafe code (LINK)Pizza 是 Scala 的前身 ->在函数类型中。我记得 C++ 程序员对这种符号选择感到困惑。这就是 Scala 选择 => 的主要原因。 AFAIRC。看来 C# 人员也经历了同样的推理。
Pizza, which was sort of a predecessor for Scala had -> in function types. I remember that C++ programmers were baffled by this choice of symbol. That's the primary reason why Scala chose => AFAIRC. It seems the C# guys went through the same reasoning.
在 Scala 中,还有“映射箭头”,表示从一个项目
a
到另一个b
的映射。对 lambda 使用=>
可以释放->
来处理诸如Map(1 -> "One", 2 -> "Two")< /code>,正如其他人指出的那样,
=>
并不是 C# 和 Scala 所独有的。In Scala, there is also the "map arrow" that indicates a mapping from an item
a
to anotherb
. Using=>
for lambdas frees up->
for things likeMap(1 -> "One", 2 -> "Two")
, and as others have pointed out the=>
is not unique to C# and Scala.您可能有兴趣知道,在 Scala 中,您可以使用 => 的 unicode 等效项。以及其他箭头: http://scala-programming-language.1934581.n4.nabble.com/More-unicode-alternatives-for-ASCII-operators-td2008146.html
you might be interested to know that in Scala you can use the unicode equivalent for => as well as other arrows: http://scala-programming-language.1934581.n4.nabble.com/More-unicode-alternatives-for-ASCII-operators-td2008146.html