scala call-by-name

scalaデフォルトはcall-by-value. => を使うと、call-by-name.


下記は、loopがすぐに評価されずに無限ループにならない例.

scala> def loop: Int = loop
loop: Int

scala> def first(x: Int, y: => Int) = x   
first: (Int,=> Int)Int

scala> first(3, loop)                  
res0: Int = 3

ポイントは、 def first(x: Int, y: => Int) = x
の[y: => Int]
def first(x: Int, y: Int) = x だと無限ループに

参考

http://www.scala-lang.org/docu/files/ScalaByExample.pdf

よし。体調が戻った.