问题
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:
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;;
来源:https://stackoverflow.com/questions/14929470/is-possible-to-define-same-named-operator-for-different-argument-count