可以为不同的参数计数定义同名运算符吗? [英] Is possible to define same-named operator for different argument count?
本文介绍了可以为不同的参数计数定义同名运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以为不同的参数计数定义同名运算符?
Is possible to define same-named operator for different argument count?
如果可能的话,怎么办?
And if it is possible then how?
例如我想要的
let (-) x y = x - y
let (-) x = -x
可悲的是,我不能只称-x,我需要使用(-)x才能使用它,但这是又一个与主要问题无关的子问题.
推荐答案
使用let绑定是不可能的
This is not possible with let bindings
来自 MSDN
您可以通过这种方式重新定义常规算术运算符 因为F#的作用域规则规定了新定义的运算符 优先于内置运算符.
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.
但是,您可以将static member (+)
版本用于过载(同一MSDN页面)
However, you can use the static member (+)
versions with overloading (Same MSDN page)
尤其有效:
> type t() =
- static member (+) (a, b) = 1
- static member (+) a = 5;;
这篇关于可以为不同的参数计数定义同名运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文