ironic
ironic

Reputation: 8959

Why this F# code with member constraint does not compile?

For me it seems that compiler has all information for that point, but I get error message. Why?

let inline getLength< ^a when ^a : (member Length : int ) > (x: ^a) = x.Length

Upvotes: 2

Views: 96

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243126

To call a member required by a static constraint, you need to use a more involved syntax:

let inline getLength< ^a when ^a : (member Length : int ) > (x: ^a) = 
  (^a : (member Length : int) x)

This is a bit ugly - which I think emphasizes the point that static member constraints are not the primary way of achieving things (often, you can use e.g. interfaces or other more usual techniques instead).

Also, if you are interested mainly in mathematical code, then you can just use standard operators and functions (together with a few primitives in LanguagePrimitives) and you won't have to invoke members explicitly.

Upvotes: 6

Related Questions