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

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -