在类型定义之外实现相等
我有几种实现接口的类型。这些类型的相等性仅取决于接口成员。是否可以为这些类型定义一次相等性,而不覆盖每种类型的 Equals
或 op_Equality
?
编辑
我尝试了以下操作,但是,无论出于何种原因,它都会覆盖 =
的每次使用,即使对于未实现 IEntity
的类型也是如此。
[<AutoOpen>]
module Equality =
let inline op_Equality (left:IEntity) (right:IEntity) = true
我还尝试使用灵活类型(#IEntity
)。相同的结果。
I have several types that implement an interface. Equality for these types only depends on interface members. Is it possible to define equality for these types once, without overriding Equals
or op_Equality
for each type?
EDIT
I tried the following, but, for whatever reason, it overrode every use of =
, even for types not implementing IEntity
.
[<AutoOpen>]
module Equality =
let inline op_Equality (left:IEntity) (right:IEntity) = true
I also tried using flexible types (#IEntity
). Same result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要做的是 mixin 或类型类可能在其他语言中启用的事情;不幸的是,F# 中没有等效的功能。您最好的选择可能是以下选项之一:
在类型之外编写相等方法,然后让所有实现都遵循它。例如,
除了一些样板文件之外,这里的缺点是,如果其他人实现了您的
IEntity
接口,他们不会被迫使用您的相等方法 - 这是选择加入。创建另一个用于
IEntity
相等性测试的运算符:这样做的(巨大)缺点是,包含 IEntity 的类型(例如元组、记录等)的结构相等不会使用此运算符来比较这些组件,这很可能导致令人惊讶的损坏代码。
What you're trying to do is something that mixins or typeclasses might enable in other languages; unfortunately there isn't equivalent functionality in F#. Your best bet is probably one of the following options:
Write your equality method outside of your type and then have all of your implementations defer to it. For example,
In addition to a bit of boilerplate, the downside here is that if anyone else implements your
IEntity
interface, they aren't forced to use your equality method - it's opt-in.Create an another operator which you use for equality testing of
IEntity
s:The (huge) downside of this is that structural equality of types containing
IEntity
s (such as tuples, records, etc.) won't use this operator to compare those components, which is likely to lead to surprising broken code.我认为没有办法以静态方式做到这一点。问题是扩展成员(例如,如果您添加
op_Equality
作为扩展)会被静态成员约束忽略(例如,如果您还重新定义< code>= 使用inlin
并满足op_Equality
要求)。F# 编译器具有一些特殊功能,仅在编译 FSharp.Core.dll 时可用(搜索源代码以查找声明
let inline GenericOne
)。它使用类似静态类型开关的东西 - 但这不能被普通人访问。因此,我没有比使用动态类型测试更好的想法,这实际上并不是一个好的方法,最好定义一个自定义运算符来比较您的接口。
作为参考,丑陋的动态方法是:
I don't think there is a way to do this in a static way. The problem is that extension members (e.g. if you added
op_Equality
as an extension) are ignored by static member constraints (e.g. if you also redefined=
usinginlin
withop_Equality
requirement).The F# compiler has some special powers available only when compiling FSharp.Core.dll that could help (search sources for the declaration
let inline GenericOne
). It uses something like static type switch - but this cannot be accessed by mere mortals.So, I don't have any idea better than using dynamic type test, which isn't really a good approach and it's probably better to define a custom operator for comparison of your interfaces.
For a reference, the ugly dynamic approach would be: