Functions without arguments, with unit as argument in scala

后端 未结 3 1885
無奈伤痛
無奈伤痛 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:16

    ()=>Int is Function0[Int] while Unit=>Int is Function1[Unit,Int]

    scala> val function0: () => Int = () => 5
    function0: () => Int = <function0>
    
    scala> val function1: Unit => Int = u => 5
    function1: (Unit) => Int = <function1>
    
    scala> function0()
    res0: Int = 5
    
    scala> function1("anything")
    res1: Int = 5
    
    scala> function1(100)
    res2: Int = 5
    
    scala>
    

    Also note that () is an object of Unit

    scala> function1(())
    res11: Int = 5
    
    scala> function1 ()
    res12: Int = 5
    
    scala> function1()
    res13: Int = 5
    
    scala> val unit = ()
    unit: Unit = ()
    
    
    scala> function1(unit)
    res15: Int = 5
    
    scala> function1 apply unit
    res16: Int = 5
    
    scala>
    
    0 讨论(0)
  • 2020-12-07 16:19

    Scala distinguishes between the following things:

    • Functions/methods with no parameter lists ("by-name parameter" if a function)
    • Functions with one empty parameter list
    • Functions with one parameter of type Unit

    None of these are equivalent, although as a convenience Scala allows you to elide empty parameter lists. (Incidentally, two empty parameter lists are also not the same.)

    So, even though Unit is written (), this is not the same as the function argument parens () for a function or method. Instead, think of () as a Tuple0.

    So, if you say f: Unit => Int, what you mean is "f takes one parameter, but it's a really boring parameter because it is Unit, which must always be the same boring Tuple0 value ()". What you're writing is really short for f: (Unit) => Int.

    If you say f: () => Int, then you mean that "f takes no parameters and produces an Int".

    If you say f: => Int, then you mean that "delay the execution of whatever statement produces an Int value until we use it in this code (and re-evaluate it each time)". Functionally, this ends up being basically the same as f: () => Int (and internally is converted into the same Function0 class), but it has a different usage, presumably to allow for a more compact form of closures (you always omit the => in the calling code).

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题