2

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

And if it is possible then how?

for example I want:

let (-) x y = x - y
let (-) x  = -x

Sadly I can't call just -x, I need (-)x to use it but it's yet another sub-question which have no relation with primary question.

1
  • 1
    The word you're looking for is 'overload', as in, you'd like to know if you can overload operators in F#. (I don't know F#, so I can't answer that part, sorry - but maybe a google search on 'overload F#' will work?)
    – Joe
    Feb 18, 2013 at 4:46

1 Answer 1

2

This is not possible with let bindings

From MSDN

You can redefine the regular arithmetic operators in this manner because the scoping rules for F# dictate that newly defined operators take precedence over the built-in operators.

However, you can use the static member (+) versions with overloading (Same MSDN page)

In particular, this works:

> type t() =                   
-  static member (+) (a, b) = 1
-  static member (+) a = 5;;   
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.