Why should successive arguments involving method application be parenthesized?

前端 未结 1 604
醉话见心
醉话见心 2021-02-19 08:15

Suppose the following F# function:

let f (x:int) (y:int) = 42

I suspect that the reason I need to parenthesize the arguments in example z2 belo

1条回答
  •  渐次进展
    2021-02-19 08:46

    I think that the problem here is that the code could be treated as:

    let z3 = f 1 rng.Next (5)
    

    This would be equivalent to omitting the parentheses and so it would be calling f with 3 arguments (the second being a function value). This sounds a bit silly, but the compiler actually does not strictly insist on having a space between parameters. For example:

    let second a b = b
    add 5(1)          // This works fine and calls 'add 5 1'
    add id(1)         // error FS0597
    add rng.Next(5)   // error FS0597
    add (rng.Next(5)) // This works fine (partial application)
    

    I think the problem is that if you look at the sequence of the 4 examples in the above snippet, it is not clear which behavior should you get in the second and the third case.

    The call rng.Next(5) is still treated in a special way, because F# allows you to chain calls if they are formed by single-parameter application without space. For example rng.Next(5).ToString(). But, for example, writing second(1)(2) is allowed, but second(1)(2).ToString() will not work.

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