开发者

Named arguments of a constructor in a type using F#

Consider the following code:

type Test(a) =
  membe开发者_StackOverflow社区r o.A = a

let test = Test(a = cos 5.)
let test2 = Test(a = 5. |> cos) // ERROR
let test3 = Test(a = (5. |> cos))

Test2 line gives an error:

The type 'bool' does not support any operators named 'Cos'

and

The value or constructor 'a' is not defined

I understand the error message but I wonder is it not a bug?


think it is ok, since precedence of (|>) is lesser than (=) expression

Test(a = 5. |> cos) 

is intepreted as

Test((a = 5.) |> cos) 

and is this case error message is correct


The F# parser treats named arguments as equality test expressions; a later stage of the compiler decodes them into named arguments. Thus it is a precedence issue as described by @desco.

Note that if you have a boolean named parameter, you can do e.g.

F(a = true)    // named param
F((a = true))  // compare local name 'a', then pass boolean as first arg

as a way to disambiguate in the rare case it is needed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜