Types F#:接口系统。i可比较

Types F#:接口系统。i可比较,types,interface,f#,overriding,icomparable,Types,Interface,F#,Overriding,Icomparable,我是F#的初学者,我不知道什么是F#以及如何使用接口。 我正在看Expert F#3.0第219页中的示例 /// A type abbreviation indicating we're using integers for unique stamps /// on objects type stamp = int /// A structural type containing a function that can't be compared for equality [<Custo

我是F#的初学者,我不知道什么是F#以及如何使用接口。 我正在看Expert F#3.0第219页中的示例

/// A type abbreviation indicating we're using integers for unique stamps
/// on objects
type stamp = int
/// A structural type containing a function that can't be compared for equality
[<CustomEquality; CustomComparison>]
type MyThing =
{Stamp : stamp;
Behaviour : (int -> int)}
override x.Equals(yobj) =
match yobj with
| :? MyThing as y -> (x.Stamp = y.Stamp)
| _ -> false
override x.GetHashCode() = hash x.Stamp
interface System.IComparable with
member x.CompareTo yobj =
match yobj with
| :? MyThing as y -> compare x.Stamp y.Stamp
| _ -> invalidArg "yobj" "cannot compare values of different types"
所以我在我的源文件中写了这个:

[<CustomEquality; CustomComparison>]
type antiint = 
     int

     override x.Equals(yobj) =
       match yobj with
         | :? antiint as y -> (x = y)
         | _ -> false
     interface System.IComparable with
       member x.CompareTo yobj =
           match yobj with
             | :? antiint as y -> anticompare x y
             | _ -> invalidArg "yobj" "cannot compare values of different types" 
[]
类型antiint=
int
覆盖x.Equals(yobj)=
将yobj与
| :? 反整数为y->(x=y)
|_u->false
接口系统.i可与
成员x.与yobj相比=
将yobj与
| :? 反整数为y->反比较x y
|->invalidArg“yobj”无法比较不同类型的值
但它不起作用。。。编译器以红色覆盖为基础,表示“类型定义中存在意外的关键字“override”。应为“|”或其他标记”


另外,我之所以要创建此类型,是因为我想使用类型优先级队列来提取最大值,而不是如注释中所述提取最小值,如果您将
antiint
定义为类型缩写(也称为类型别名),则无法覆盖其成员,也无法更改有关该类型的任何内容

最好的方法可能是将其定义为一个值类型(struct),它是
int
上的一个薄包装。大概是这样的:

[<Struct; CustomEquality; CustomComparison>]
type antiint(value:int) = 
     member x.Value = value
     override x.Equals(yobj) =
       match yobj with
         | :? antiint as y -> (x = y)
         | _ -> false
     interface System.IComparable with
       member x.CompareTo yobj =
           match yobj with
             | :? antiint as y -> anticompare x y
             | _ -> invalidArg "yobj" "cannot compare values of different types" 
[]
类型antiint(值:int)=
成员x.值=值
覆盖x.Equals(yobj)=
将yobj与
| :? 反整数为y->(x=y)
|_u->false
接口系统.i可与
成员x.与yobj相比=
将yobj与
| :? 反整数为y->反比较x y
|->invalidArg“yobj”无法比较不同类型的值

这将被编译为值类型,因此性能开销应该最小。不幸的是,您将无法对上述类型使用标准的数字运算符,因此您可以使用
Value
属性编写计算,或者添加运算符,如注释中所述。

中所述,如果您将
antiint
定义为类型缩写(也称为类型别名),您不能重写其成员,也不能更改有关该类型的任何内容

最好的方法可能是将其定义为一个值类型(struct),它是
int
上的一个薄包装。大概是这样的:

[<Struct; CustomEquality; CustomComparison>]
type antiint(value:int) = 
     member x.Value = value
     override x.Equals(yobj) =
       match yobj with
         | :? antiint as y -> (x = y)
         | _ -> false
     interface System.IComparable with
       member x.CompareTo yobj =
           match yobj with
             | :? antiint as y -> anticompare x y
             | _ -> invalidArg "yobj" "cannot compare values of different types" 
[]
类型antiint(值:int)=
成员x.值=值
覆盖x.Equals(yobj)=
将yobj与
| :? 反整数为y->(x=y)
|_u->false
接口系统.i可与
成员x.与yobj相比=
将yobj与
| :? 反整数为y->反比较x y
|->invalidArg“yobj”无法比较不同类型的值

这将被编译为值类型,因此性能开销应该最小。不幸的是,您将无法对上述类型使用标准的数字运算符,因此您可以使用
Value
属性编写计算,或者添加运算符,如中所述。

type antiint=int
不是类型定义,而是类型缩写。您只是给int类型赋予了一个新名称。编译器不会将以
override
开头的下一行识别为任何类型的重写,因为它在语义上与以前的类型无关lines@petr我懂了。那么我该怎么办呢?您不能继承像
int
这样的值类型。如果需要创建具有自定义行为的数字类型,则需要进行一些非平凡的工作。参见此处示例:
type antiint=int
不是类型定义,而是类型缩写。您只是给int类型赋予了一个新名称。编译器不会将以
override
开头的下一行识别为任何类型的重写,因为它在语义上与以前的类型无关lines@petr我懂了。那么我该怎么办呢?您不能继承像
int
这样的值类型。如果需要创建具有自定义行为的数字类型,则需要进行一些非平凡的工作。请参见此处的示例: