Why does Scala apply thunks automatically, sometimes?

前端 未结 4 687
花落未央
花落未央 2021-02-05 03:39

At just after 2:40 in ShadowofCatron\'s Scala Tutorial 3 video, it\'s pointed out that the parentheses following the name of a thunk are optional. \"B

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-05 04:17

    The default cause, when you write:

    val b = f
    

    is to evaluate the function and assign the result to b, as you have noticed. You can use the _, or you can explicitly specify the type of b:

    // These all have the same effect
    val b = f _
    val b: () => Int = f
    val b: Function0[Int] = f
    

提交回复
热议问题