如何在F#中为自定义类型定义一元减号?

问题描述 投票:0回答:1

如何在F#中为自定义类型定义-x

这将抱怨它期望一个元组:

type Vector2<'T> = 
  | Vector2 of 'T * 'T
  with
    static member inline (-) (Vector2 (x, y)) =
      Vector2 (-x, -y)
f#
1个回答
0
投票

您需要将~字符添加到运算符定义中。

static member inline (~-) (Vector2 (x, y)) = Vector2 (-x, -y)

这将告诉编译器是一元运算符。

© www.soinside.com 2019 - 2024. All rights reserved.