目前知道的基本和匿名函数定义和函数类型声明有关,以及在case语句中的使用
case
语句中
case x => y
不过stackoverflow 上关于这方面的讨论挺多的,故在此记录一下
比如来自 http://stackoverflow.com/questions/6951895/what-does-and-mean-in-scala 的一位的回复:
=> has several meanings in Scala, all related to its mathematical meaning as implication.
-
In a value, it introduces a function literal, or lambda. e.g. the bit inside the curly braces in
List(1,2,3).map { (x: Int) => x * 2 }(匿名函数定义) -
In a type, with symbols on both sides of the arrow (e.g.
A => T,(A,B) => T,(A,B,C) => T, etc.) it's sugar forFunction<n>[A[,B,...],T], that is, a function that takes parameters of typeA[,B...], and returns a value of typeT.(函数类型定义)-
Empty parens on the left hand side (e.g.
() => T) indicate that the function takes no parameters (also sometimes called a "thunk"); -
Empty parens on the right hand side denote that it returns
()—the sole value of typeUnit, whose name can also be written()—confused yet? :)A function that returns Unit is also known as a procedure, normally a method that's called only for its side effect.
-
-
In the type declaration for a method or function parameter, with no symbol on the left hand side (e.g.
def f(param: => T)) it's a "by-name parameter", meaning that is evaluated every time it's used within the body of the function, and not before. Ordinary "by-value" parameters are evaluated before entry into the function/method.(函数类型定义) -
In a
caseclause, they separate the pattern (and optional guard) from the result expression, e.g.case x => y.(case 语句)
http://stackoverflow.com/questions/6951895/what-does-and-mean-in-scala
http://stackoverflow.com/questions/7888944/scala-punctuation-aka-symbols-and-operators
本文探讨了Scala编程语言中箭头符号的不同用途,包括匿名函数定义、函数类型声明、by-name参数以及在case语句中的应用。此外,还介绍了这些概念与数学中的蕴含概念之间的联系。
709

被折叠的 条评论
为什么被折叠?



