Scala の メソッド は、
・引数が0個の場合は、メソッド名の後ろの ( ) を省略できる。
・オブジェクト と メソッド と 引数 を 演算子 の様に中間記法で表記できる。
・演算子 を メソッド呼び出しの表記法で利用できる。(実は演算子は ただのメソッド)
・メソッドの部分適用から関数オブジェクトを得ることが出来る。
scala> def a() = 123a: ()Intscala> a()res0: Int = 123scala> ares1: Int = 123scala> object o { def a()=234; def b(x:Int) = x}defined object oscala> o.ares2: Int = 234scala> o.b(345)res3: Int = 345scala> o b 345res4: Int = 345scala> o.b(_:Int)
res6: Int => Int = <function1>scala> o.b(_:Int)(456)<console>:9: error: Int does not take parameters o.b(_:Int)(456) ^scala> (o.b(_:Int))(456)res8: Int = 456scala> object oo { def c(x:Int,y:Int) = x-y}defined object ooscala> oo.c(_:Int,_:Int)res9: (Int, Int) => Int = <function2>scala> oo.c(_:Int,_:Int).curried<console>:9: error: value curried is not a member of Int oo.c(_:Int,_:Int).curried ^scala> (oo.c(_:Int,_:Int)).curriedres11: Int => (Int => Int) = <function1>scala> (oo.c(_:Int,_:Int)).curried 1 2<console>:1: error: ';' expected but integer literal found. (oo.c(_:Int,_:Int)).curried 1 2 ^scala> (oo.c(_:Int,_:Int)).curried (1) (2)res12: Int = -1scala> object ooo { def c(x:Int)(y:Int) = x-y}defined object oooscala> ooo.c (1)
<console>:9: error: missing arguments for method c in object ooo;follow this method with `_' if you want to treat it as a partially applied function
ooo.c (1) ^scala> ooo.c (1) (2)res15: Int = -1scala> (ooo.c (1)) (2)res16: Int = -1scala> ooo.c(1) (_:Int)res17: Int => Int = <function1>scala> val aaa = ooo.c(1) (_:Int)
aaa: Int => Int = <function1>scala> aaa (1)res18: Int = 0Stream// 0 1 2 3 の遅延ストリームStream.from(0)val ls = List(1,2,3)val ss = Stream(4,5,6)scala> ls ::: lsList[Int] = List(1, 2, 3, 1, 2, 3)scala> ls.toStream #::: ssres5: scala.collection.immutable.Stream[Int] = Stream(1, ?)関数の引数の名前渡しで遅延評価def f( a:Int , b: => Int)