Named constructor arguments in a type using F #

Consider the following code:

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

The line Test2 gives an error:

Type "bool" does not support operators named "Cos"

and

Value or constructor 'a' not defined

I understand the error message, but I wonder is this not a mistake?

+4
source share
2 answers

The F # parser treats named arguments as expressions for checking equality; a later stage of the compiler decodes them into named arguments. So this is the priority issue described by @desco.

Note that if you have a logical named parameter, you can do this, for example.

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

as a way to eliminate ambiguity in the rare case, it is necessary.

+1
source

I think this is normal, since priority (|>) is less than (=) expression

 Test(a = 5. |> cos) 

interpreted as

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

and in this case the error message is correct

+5
source

Source: https://habr.com/ru/post/1334898/


All Articles