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

寵の児 提交于 2019-12-10 22:45:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!