如何在F#中为自定义类型定义-x
?
这将抱怨它期望一个元组:
type Vector2<'T> =
| Vector2 of 'T * 'T
with
static member inline (-) (Vector2 (x, y)) =
Vector2 (-x, -y)
您需要将~
字符添加到运算符定义中。
static member inline (~-) (Vector2 (x, y)) = Vector2 (-x, -y)
这将告诉编译器是一元运算符。