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?
I don’t think there is a straightforward way to do this in general. You could probably use an
inlineoperator 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:
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:
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…