Functions without arguments, with unit as argument in scala

后端 未结 3 1899
無奈伤痛
無奈伤痛 2020-12-07 15:31
def foo(x: Int, f: Unit => Int) = println(f())

foo(2, { Unit => 3 + 4 })

// case 1
def loop: Int = 7
foo(2, loop) // does not compile

changing loop to 
// c         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 16:33

    In case 1 and 2 above, the return value of loop rather than loop itself is type checked for the second argument to foo and fails: Int != Unit => Int

    The change to loop has a typo.

提交回复
热议问题