Why does Scala apply thunks automatically, sometimes?

前端 未结 4 695
花落未央
花落未央 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条回答
  • 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
    
    0 讨论(0)
  • 2021-02-05 04:25

    Your guess is correct - Scala has type-dependent semantics regarding the evalution of expressions.

    Like Ruby, it always evaluates thunks even without parentheses. (This might have advantages for interaction purposes since you can switch pure and possibly impure operations without having to change the syntax.)

    But since Scala has a powerful static type system, it can break the above rule and save the programmer from explicitly partially applying the functions in cases where the result of an evalution wouldn't make sense from a type perspective.

    Note that type-dependent evalution can even simulate call-by-name


    Now is type-dependent evalution behaviour is good or bad? ... Well, it can certainly lead to confusing cases like yours and doesn't feel that pure any more. But in most cases, it just works as intended by the programmer (making code more concise) - So let's say, it's okay.

    0 讨论(0)
  • 2021-02-05 04:29

    In your example

    def f(): Int = { counter = counter + 1; counter }
    

    is defining a method, not a function. AFAIK methods are promoted to functions automatically in Scala depending on context. To define a function you can write

    val f = () => { counter = counter + 1; counter }
    

    and I think you will get what you want.

    0 讨论(0)
  • 2021-02-05 04:34

    The problem is here:

    Buh?" said my functional programming brain, since the value of a function and the value it evaluates to when applied are completely different things.

    Yes, but you did not declare any function.

    def f(): Int = { counter = counter + 1; counter }
    

    You declared a method called f which has one empty parameter list, and returns Int. A method is not a function -- it does not have a value. Never, ever. The best you can do is get a Method instance through reflection, which is not really the same thing at all.

    val b = f _     // Hey Scala, I mean f, not f()
    

    So, what does f _ means? If f was a function, it would mean the function itself, granted, but this is not the case here. What it really means is this:

    val b = () => f()
    

    In other words, f _ is a closure over a method call. And closures are implemented through functions.

    Finally, why are empty parameter lists optional in Scala? Because while Scala allows declarations such as def f = 5, Java does not. All methods in Java require at least an empty parameter list. And there are many such methods which, in Scala style, would not have any parameters (for example, length and size). So, to make the code look more uniform with regards to empty parameter list, Scala makes them optional.

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