Custom comparison #13177
-
Hi! type Nat =
private | Nat of int
static member One = Nat 1
static member New (x: int) : Nat option =
if x > 0
then Some (Nat x)
else None
static member (+) (Nat a, Nat b) = Nat (a + b)
static member (-) (Nat a, Nat b) = Nat.New (a - b)
static member (*) (Nat a, Nat b) = Nat (a * b)
static member (/) (Nat a, Nat b) = Nat (a / b) Since this type is a simple static member (+) (Nat a, b: int) = Nat.New (a + b) Now consider you have an object static member (=) (Nat a, b: int) = a = b // Error Is there any workaround to do something like that? Thanks in advance! P.S. While it's perfectly possible to define a custom operator or a function (e.g. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
There is no direct way to overload |
Beta Was this translation helpful? Give feedback.
There is no direct way to overload
=
in F#. You could define your own operator. You might also get some mileage from anop_Implicit
. However neither are really that recommended (they make code harder to understand, and you should probably just really accept a tax of an explicit coercion for convertingint
-->Nat
.