Scala Call By Name Confusion

前端 未结 3 1295
渐次进展
渐次进展 2021-01-05 16:48

I am working on some call by name examples using the REPL and running the same examples in Eclipse.

Here is what in Eclipse:
Scenario 1:

val fun         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-05 17:30

    Scenario 1: As expected. The output is from the first line, not calling the method.

    Scenario 2: I don't have Eclipse, but doesn't sound right.

    REPL Scenario 1: you can't define a call by name value. what you did is assign the last value of the expression {println("Calling funct")} to funct. That last value is the result of println, which is Unit. So takesFunct receives a thunk that evaluates to just Unit, so nothing is printed.

    REPL Scenario 2: As expected

提交回复
热议问题