Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement C# equality operator from F#

Is it possible to provide an implementation for the C# equality (==) operator in F#?

EDIT:

This code:

static member (=) (left : Foo, right : Foo) =

Produces the compiler warning:

The name '(=)' should not be used as a member name. To define equality semantics for a type, override the 'Object.Equals' member. If defining a static member for use from other CLI languages then use the name 'op_Equality' instead.

The latter part addresses what I was trying to do. Any idea why op_Equality is favored over simply (=)?

like image 895
Daniel Avatar asked Nov 12 '10 19:11

Daniel


2 Answers

Figured it out:

type Foo() =
    static member op_Equality (left : Foo, right : Foo) =
like image 55
Daniel Avatar answered Nov 16 '22 02:11

Daniel


Are you looking for operator overloading in F#?

The different operators are defined in CIL, so overloading loading an operator in F# would overload it in C#, VB.NET and any other .NET language that supports that operator.

like image 21
Oded Avatar answered Nov 16 '22 02:11

Oded