Is there a change in the lazy identifier of the new Scala? -
def maybetwice(b:boolean,i: =>int) = { val j = if (b) j+j else 0 } def maybetwice(b:boolean,i: =>int) = { lazy val j = if (b) j+j else 0 } val x=stream.maybetwice(true,{println("hi");41+1})
the above code execution same result, not described in functional programming in scala book.
how differ read in book? lazy val
delays evaluation until first usage. code may same, different. when send false, j
never evaluated. may clarify:
def maybetwice(b:boolean, i:int) = { lazy val j = { println("hi"); } if (b) j+j else 0 }
from repl:
scala> maybetwice(true,10) hi res10: int = 20 scala> maybetwice(false,20) res11: int = 0
in second case, j never evaluated.
wiki
Comments
Post a Comment