fahadash
fahadash

Reputation: 3281

Is it impossible to overload post-increment operator in F#

I am writing a library for C# developers. The library is written in F#. C# developers would like to use ++ operator on one of the objects. How can I do that ?

I looked up online, found that ++ post-increment operator is a no-such-thing in F#.

Upvotes: 3

Views: 168

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243061

Even though F# does not have the ++ operator, you can still define an F# type that supports it:

type A(n:int) = 
  member x.N = n
  static member op_Increment (a:A) = A(a.N + 1)

The trick is that you have to use the op_Increment name for the method, because that's what C# uses for the ++ operator. Unfortunately, F# does not understand the operator and so if you write member (++) ..., the compiler will call the method op_PlusPlus instead.

Upvotes: 8

Related Questions