How to declare global level overload-able operators?

元气小坏坏 提交于 2019-12-24 16:23:29

问题


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?


回答1:


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

// Prefix version of the operator (when you write e.g. '+. 10')
let (~+.) a = a * 10
// Infix version of the operator (when you write e.g. '1 +. 10')
let (+.) a b = a + b

// Sample use
+. 10
10 +. 20

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:

infix-or-prefix-op :=
      +, -, +., -., %, &, &&

prefix-op :=
      infix-or-prefix-op
      ~ ~~ ~~~ (and any repetitions of ~)
      !OP (except !=)

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...




回答2:


So what about a Discriminated Union:

type RHS = 
|One of int
|Two of int * int
...

then

let (-|-) a b =
    match b with
    |One(a) -> ...
    |Two(a,b) -> ...
     ...

Call it with:

1 -|- One(1)
1 -|- Two(1,1)
....


来源:https://stackoverflow.com/questions/14930385/how-to-declare-global-level-overload-able-operators

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