「=>」scalaでの意味
現在知られている基本は匿名関数定義と関数タイプ宣言に関係し、case文での使用
=>
1、匿名関数定義、左がパラメータ右が関数実装体(x:Int)=>{}
2、関数タイプの
宣言、左はパラメータタイプ、右はメソッド戻り値タイプ(Int)=>(Int)
3、
By-name-parameter f(p :=>Int)
2との違い
参考:scala By-name-parameterとFunction typeの違い
4、
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.
List(1,2,3).map { (x: Int) => x * 2 }( )
A => T
, (A,B) => T
, (A,B,C) => T
, etc.) it's sugar for Function<n>[A[,B,...],T]
, that is, a function that takes parameters of type A[,B...]
, and returns a value of type T
.(関数タイプ定義)() => T
) indicate that the function takes no parameters (also sometimes called a "thunk"); ()
—the sole value of type Unit
, 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. 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.(関数タイプ定義)case
clause, 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