Franco Tiveron
Franco Tiveron

Reputation: 2896

F# - Unexpected behaviour in operator overloading

The following F# code compiles successfully:

namespace MyNamespace

module Module = 
    type T = 
        static member (*) (t1: T, t2: T): T = Unchecked.defaultof<T>

    type U = 
        member __.Method(t1: T, t2: T) = t1 * t2

However, if a private qualifier is added to Module, an error is generated:

namespace MyNamespace

module private Module = 
    type T = 
        static member (*) (t1: T, t2: T): T = Unchecked.defaultof<T>

    type U = 
        member __.Method(t1: T, t2: T) = t1 * t2 //error

Error FS0043 The member or object constructor 'op_Multiply' is not public. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.

This error doesn't sound right:

?

Upvotes: 3

Views: 73

Answers (1)

bmitc
bmitc

Reputation: 833

Upon a bit of searching, this appears to at least be known behavior (to a small few) with a poor error message and an existing language suggestion.

This F# issue https://github.com/dotnet/fsharp/issues/1956 points to the language suggestion https://github.com/fsharp/fslang-suggestions/issues/230.

As to why this occurs, Don Syme mentions in the above links:

"private" on a type in F# means "private to the enclosing namespace declaration group or module".

However right now all solutions to statically resolved calls must currently be public. There's an open language suggestion to lift that limitation, in which case this code would indeed compile.


Here are some related StackOverflow questions I happened to find while searching:

Upvotes: 0

Related Questions