开发者

High-precedence application expressions as arguments

开发者 https://www.devze.com 2023-04-09 00:38 出处:网络
A high precedence application expression is one in which an identifier is immediately following by a left paren without intervening whitespace, e.g., f(g). Parentheses are required when passing these

A high precedence application expression is one in which an identifier is immediately following by a left paren without intervening whitespace, e.g., f(g). Parentheses are required when passing these as function arguments: func (f(g)).

Section 15.2 of the spec states the grammar and precedence rules allow the unparenthesized form -- func f(g) -- but an additional check prevents this.

Why is this intentionally prohibited? It would obviate the need for excessive parentheses and piping, and generally make the code much cle开发者_开发技巧aner.

A common example is

raise <| IndexOutOfRangeException()

or

raise (IndexOutOfRangeException())

could become simply

raise IndexOutOfRangeException()


I agree that the need for writing the additional parentheses is a bit annoying. I think that the main reason why it is not allowed to omit them is that adding a whitespace would then change the meaning of your code in quite a significant way:

// Call 'foo' with the result of 'bar()' as an argument
foo bar()

// Call 'foo' with 'bar' as the first argument and '()' as the second 
foo bar ()

There are still some rough edges where adding parens changes the evaluation (see this form post), but that "just" changes the evaluation order. This would change the meaning of your code!

0

精彩评论

暂无评论...
验证码 换一张
取 消