且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么要在涉及方法应用的连续参数中加上括号?

更新时间:2022-11-23 09:23:58

我认为这里的问题是代码可以被视为:

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

let z3 = f 1 rng.Next (5)

这等效于省略括号,因此它将使用3个参数(第二个是函数值)调用f.这听起来有些愚蠢,但是编译器实际上并不严格要求在参数之间留有空格.例如:

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)

我认为问题在于,如果您看一下上面片段中的4个示例的顺序,则不清楚在第二种情况和第三种情况下应该获得哪种行为.

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.

仍然以特殊方式处理呼叫rng.Next(5),因为如果由单参数应用程序形成的呼叫没有空间,则F#允许您将其链接起来.例如rng.Next(5).ToString().但是,例如,允许写second(1)(2),但是second(1)(2).ToString()将不起作用.

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.