【发布时间】:2019-12-23 05:58:08
【问题描述】:
我想为相等和比较函数创建一个默认实现的接口。
如果我从IKeyable<'A> 类型中删除除Key 成员之外的所有内容,只要我不添加默认实现,它就是一个有效接口。从IKeyable<'A> 中删除其他接口实现,只保留默认成员会得到相同的结果。
type IKeyable<'A when 'A: equality and 'A :> IComparable> =
abstract member Key : 'A
default this.Equals obj = // hidden for clarity
default this.GetHashCode () = // hidden for clarity
interface IEquatable<'A> with
member this.Equals otherKey = // hidden for clarity
interface IComparable<'A> with
member this.CompareTo otherKey = // hidden for clarity
interface IComparable with
member this.CompareTo obj = // hidden for clarity
type Operation =
{ Id: Guid }
interface IKeyable<Guid> with // Error: The type 'IKeyable<Guid>' is not an interface type
member this.Key = this.Id
我想使用IKeyable<'A> 作为接口,以便“获得”相等和比较的默认实现。
错误消息出现在interface ... with 类型Operation 下:The type 'IKeyable<Guid>' is not an interface type
【问题讨论】:
标签: interface f# default-method