运算符重载

本主题介绍如何在类或记录类型以及全局级别重载算术运算符。

语法

// Overloading an operator as a class or record member.
static member (operator-symbols) (parameter-list) =
    method-body
// Overloading an operator at the global level
let [inline] (operator-symbols) parameter-list = function-body

注解

在前面的语法中, 运算符符号 是其中 +之一、 -*/、, =等等。 参数列表按作数在运算符的常用语法中显示的顺序指定作数。 方法主体构造生成的值。

运算符的运算符重载必须是静态的。 一元运算符(如+-)的运算符重载必须使用运算符符号中的波形符(~),以指示运算符是一元运算符而不是二元运算符,如以下声明所示。

static member (~-) (v : Vector)

下面的代码演示了一个仅包含两个运算符的向量类,一个用于一元减数,一个用于乘量。 在此示例中,需要两个标量乘重的重载,因为无论矢量和标量出现的顺序如何,运算符都必须工作。

type Vector(x: float, y : float) =
   member this.x = x
   member this.y = y
   static member (~-) (v : Vector) =
     Vector(-1.0 * v.x, -1.0 * v.y)
   static member (*) (v : Vector, a) =
     Vector(a * v.x, a * v.y)
   static member (*) (a, v: Vector) =
     Vector(a * v.x, a * v.y)
   override this.ToString() =
     this.x.ToString() + " " + this.y.ToString()

let v1 = Vector(1.0, 2.0)

let v2 = v1 * 2.0
let v3 = 2.0 * v1

let v4 = - v2

printfn "%s" (v1.ToString())
printfn "%s" (v2.ToString())
printfn "%s" (v3.ToString())
printfn "%s" (v4.ToString())

输出:

1 2
2 4
2 4
-2 -4

创建新运算符

可以重载所有标准运算符,但也可以根据特定字符序列创建新的运算符。 允许的运算符字符包括!:、%$&+.-/?=<*@^>|和。~~ 字符具有使运算符一元化的特殊含义,并且不是运算符字符序列的一部分。 并非所有运算符都可以一元化。

根据使用的确切字符序列,运算符将具有特定的优先级和关联性。 关联性可以是从左到右或从右到左,并且每当具有相同优先级级别的运算符按顺序出现时使用,而不带括号。

运算符字符 . 不会影响优先级,因此,例如,如果要定义与普通乘法具有相同优先级和关联性的乘法版本,则可以创建运算符,例如 .*

运算符 $ 必须独立且没有其他符号。

可在 符号和运算符引用中找到 F# 中所有运算符的优先级的表。

重载运算符名称

当 F# 编译器编译运算符表达式时,它将生成一个方法,该方法具有该运算符的编译器生成名称。 这是方法的公共中间语言(CIL)以及反射和 IntelliSense 中显示的名称。 通常不需要在 F# 代码中使用这些名称。

下表显示了标准运算符及其相应的生成名称。

操作员 生成的名称
[] op_Nil
:: op_Cons
+ op_Addition
- op_Subtraction
* op_Multiply
/ op_Division
@ op_Append
^ op_Concatenate
% op_Modulus
&&& op_BitwiseAnd
||| op_BitwiseOr
^^^ op_ExclusiveOr
<<< op_LeftShift
~~~ op_LogicalNot
>>> op_RightShift
~+ op_UnaryPlus
~- op_UnaryNegation
= op_Equality
<= op_LessThanOrEqual
>= op_GreaterThanOrEqual
< op_LessThan
> op_GreaterThan
? op_Dynamic
?<- op_DynamicAssignment
|> op_PipeRight
<| op_PipeLeft
! op_Dereference
>> op_ComposeRight
<< op_ComposeLeft
<@ @> op_Quotation
<@@ @@> op_QuotationUntyped
+= op_AdditionAssignment
-= op_SubtractionAssignment
*= op_MultiplyAssignment
/= op_DivisionAssignment
.. op_Range
.. .. op_RangeStep

请注意, not F# 中的运算符不会发出 op_Inequality ,因为它不是符号运算符。 它是一个发出 IL 的函数,它否定布尔表达式。

此处未列出的运算符字符的其他组合可用作运算符,并具有由下表中各个字符连接名称构成的名称。 例如,+! 将变为 op_PlusBang

运算符字符 名称
> Greater
< Less
+ Plus
- Minus
* Multiply
/ Divide
= Equals
~ Twiddle
$ Dollar
% Percent
. Dot
& Amp
| Bar
@ At
^ Hat
! Bang
? Qmark
( LParen
, Comma
) RParen
[ LBrack
] RBrack

前缀和 Infix 运算符

前缀 运算符应放置在作数或作数的前面,这与函数非常类似。 应将 infix 运算符放置在两个作数之间。

只有某些运算符可用作前缀运算符。 某些运算符始终是前缀运算符,其他运算符可以是不合符或前缀,其余运算符始终为不合符运算符。 以 !除运算符和运算符开头 !=的运算符 ~(或重复序列) ~始终为前缀运算符。 运算符+-+.-.&&&、和%%%可以是前缀运算符或 infix 运算符。 通过在定义前缀运算符时添加前缀运算符的开头来 ~ 区分这些运算符的前缀版本。 ~仅在定义运算符时不使用该运算符。

示例:

以下代码演示如何使用运算符重载来实现分数类型。 分数由分子和分母表示。 该函数 hcf 用于确定用于减少分数的最高常见因子。

// Determine the highest common factor between
// two positive integers, a helper for reducing
// fractions.
let rec hcf a b =
  if a = 0u then b
  elif a<b then hcf a (b - a)
  else hcf (a - b) b

// type Fraction: represents a positive fraction
// (positive rational number).
type Fraction =
   {
      // n: Numerator of fraction.
      n : uint32
      // d: Denominator of fraction.
      d : uint32
   }

   // Produce a string representation. If the
   // denominator is "1", do not display it.
   override this.ToString() =
      if (this.d = 1u)
        then this.n.ToString()
        else this.n.ToString() + "/" + this.d.ToString()

   // Add two fractions.
   static member (+) (f1 : Fraction, f2 : Fraction) =
      let nTemp = f1.n * f2.d + f2.n * f1.d
      let dTemp = f1.d * f2.d
      let hcfTemp = hcf nTemp dTemp
      { n = nTemp / hcfTemp; d = dTemp / hcfTemp }

   // Adds a fraction and a positive integer.
   static member (+) (f1: Fraction, i : uint32) =
      let nTemp = f1.n + i * f1.d
      let dTemp = f1.d
      let hcfTemp = hcf nTemp dTemp
      { n = nTemp / hcfTemp; d = dTemp / hcfTemp }

   // Adds a positive integer and a fraction.
   static member (+) (i : uint32, f2: Fraction) =
      let nTemp = f2.n + i * f2.d
      let dTemp = f2.d
      let hcfTemp = hcf nTemp dTemp
      { n = nTemp / hcfTemp; d = dTemp / hcfTemp }

   // Subtract one fraction from another.
   static member (-) (f1 : Fraction, f2 : Fraction) =
      if (f2.n * f1.d > f1.n * f2.d)
        then failwith "This operation results in a negative number, which is not supported."
      let nTemp = f1.n * f2.d - f2.n * f1.d
      let dTemp = f1.d * f2.d
      let hcfTemp = hcf nTemp dTemp
      { n = nTemp / hcfTemp; d = dTemp / hcfTemp }

   // Multiply two fractions.
   static member (*) (f1 : Fraction, f2 : Fraction) =
      let nTemp = f1.n * f2.n
      let dTemp = f1.d * f2.d
      let hcfTemp = hcf nTemp dTemp
      { n = nTemp / hcfTemp; d = dTemp / hcfTemp }

   // Divide two fractions.
   static member (/) (f1 : Fraction, f2 : Fraction) =
      let nTemp = f1.n * f2.d
      let dTemp = f2.n * f1.d
      let hcfTemp = hcf nTemp dTemp
      { n = nTemp / hcfTemp; d = dTemp / hcfTemp }

   // A full set of operators can be quite lengthy. For example,
   // consider operators that support other integral data types,
   // with fractions, on the left side and the right side for each.
   // Also consider implementing unary operators.

let fraction1 = { n = 3u; d = 4u }
let fraction2 = { n = 1u; d = 2u }
let result1 = fraction1 + fraction2
let result2 = fraction1 - fraction2
let result3 = fraction1 * fraction2
let result4 = fraction1 / fraction2
let result5 = fraction1 + 1u
printfn "%s + %s = %s" (fraction1.ToString()) (fraction2.ToString()) (result1.ToString())
printfn "%s - %s = %s" (fraction1.ToString()) (fraction2.ToString()) (result2.ToString())
printfn "%s * %s = %s" (fraction1.ToString()) (fraction2.ToString()) (result3.ToString())
printfn "%s / %s = %s" (fraction1.ToString()) (fraction2.ToString()) (result4.ToString())
printfn "%s + 1 = %s" (fraction1.ToString()) (result5.ToString())

输出:

3/4 + 1/2 = 5/4
3/4 - 1/2 = 1/4
3/4 * 1/2 = 3/8
3/4 / 1/2 = 3/2
3/4 + 1 = 7/4

全局级别的运算符

还可以在全局级别定义运算符。 以下代码定义运算符 +?

let inline (+?) (x: int) (y: int) = x + 2*y
printf "%d" (10 +? 1)

上述代码的输出为 12.

可以采用这种方式重新定义常规算术运算符,因为 F# 的范围规则决定了新定义的运算符优先于内置运算符。

关键字 inline 通常与全局运算符一起使用,这些运算符通常是最好集成到调用代码中的小型函数。 使运算符函数内联还可以使用静态解析的类型参数来生成静态解析的泛型代码。 有关详细信息,请参阅 内联函数静态解析的类型参数

另请参阅