cnd
cnd

Reputation: 33744

How to declare global level overload-able operators?

based on: Is possible to define same-named operator for different argument count?

I want to define some operator like + but let call -|- for example to have

let a = -|- 1
let b = 1 -|- 1
let c = 1 -|- 1 1 1 1

At least 2 first lines will work for + but how can I declare own operator like this?

Upvotes: 1

Views: 138

Answers (2)

Tomas Petricek
Tomas Petricek

Reputation: 243041

I don't think there is a straightforward way to do this in general. You could probably use an inline operator with a mix of static member constraints to cover some of the cases that you want, but I don't think it will be very elegant.

For some operator names, you can however define a separate prefix and infix version, which covers the first two cases:

// Prefix version of the operator (when you write e.g. '+. 10')
let (~+.) a = a * 10
// Infix version of the operator (when you write e.g. '1 +. 10')
let (+.) a b = a + b

// Sample use
+. 10
10 +. 20

You still won't be able to write 10 +. 20 30 40, because then you'd need an overloaded infix operator.

It is worth noting that you cannot do this for all operator names. Here is a syntax definition from the F# specification of the allowed operator names for infix operators:

infix-or-prefix-op :=
      +, -, +., -., %, &, &&

prefix-op :=
      infix-or-prefix-op
      ~ ~~ ~~~ (and any repetitions of ~)
      !OP (except !=)

PS: I'm not generally a big fan of custom operators - in some cases, they are nice, but they are difficult to discover (you do not see them in the IntelliSense) and unless you're using standard ones for numerical computing, it is often difficult to understand what they mean. So I would maybe consider other approaches...

Upvotes: 5

John Palmer
John Palmer

Reputation: 25516

So what about a Discriminated Union:

type RHS = 
|One of int
|Two of int * int
...

then

let (-|-) a b =
    match b with
    |One(a) -> ...
    |Two(a,b) -> ...
     ...

Call it with:

1 -|- One(1)
1 -|- Two(1,1)
....

Upvotes: 1

Related Questions