在类型定义之外实现相等

发布于 2024-10-21 12:12:22 字数 411 浏览 6 评论 0原文

我有几种实现接口的类型。这些类型的相等性仅取决于接口成员。是否可以为这些类型定义一次相等性,而不覆盖每种类型的 Equalsop_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌若浮生 2024-10-28 12:12:22

您想要做的是 mixin 或类型类可能在其他语言中启用的事情;不幸的是,F# 中没有等效的功能。您最好的选择可能是以下选项之一:

  1. 使用抽象基类而不是接口。
  2. 在类型之外编写相等方法,然后让所有实现都遵循它。例如,

    让entityEquals (i1:IEntity) (i2:IEntity) =
      i1.Member1 = i2.Member1 &&
      i1.Member2 = i2.Member2 &&
      ...
    
    类型 MyEntity() =
      接口 IEntity 与
        成员 x.Member1 = ...
        ...
        覆盖 x.Equals(y) = 
          将 y 与
          | :? IEntity 作为 y ->实体等于 xy
          | _->错误的
        覆盖 x.GetHashCode() =
          ...
    

    除了一些样板文件之外,这里的缺点是,如果其他人实现了您的 IEntity 接口,他们不会被迫使用您的相等方法 - 这是选择加入。

  3. 创建另一个用于 IEntity 相等性测试的运算符:

    let (==) (i1:IEntity) (i2:IEntity) =
      i1.Member1 = i2.Member1 &&
      ...
    

    这样做的(巨大)缺点是,包含 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:

  1. Use an abstract base class instead of an interface.
  2. Write your equality method outside of your type and then have all of your implementations defer to it. For example,

    let entityEquals (i1:IEntity) (i2:IEntity) =
      i1.Member1 = i2.Member1 &&
      i1.Member2 = i2.Member2 &&
      ...
    
    type MyEntity() =
      interface IEntity with
        member x.Member1 = ...
        ...
        override x.Equals(y) = 
          match y with
          | :? IEntity as y -> entityEquals x y
          | _ -> false
        override x.GetHashCode() =
          ...
    

    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.

  3. Create an another operator which you use for equality testing of IEntitys:

    let (==) (i1:IEntity) (i2:IEntity) =
      i1.Member1 = i2.Member1 &&
      ...
    

    The (huge) downside of this is that structural equality of types containing IEntitys (such as tuples, records, etc.) won't use this operator to compare those components, which is likely to lead to surprising broken code.

记忆里有你的影子 2024-10-28 12:12:22

我认为没有办法以静态方式做到这一点。问题是扩展成员(例如,如果您添加op_Equality作为扩展)会被静态成员约束忽略(例如,如果您还重新定义< code>= 使用 inlin 并满足 op_Equality 要求)。

F# 编译器具有一些特殊功能,仅在编译 FSharp.Core.dll 时可用(搜索源代码以查找声明 let inline GenericOne)。它使用类似静态类型开关的东西 - 但这不能被普通人访问。

因此,我没有比使用动态类型测试更好的想法,这实际上并不是一个好的方法,最好定义一个自定义运算符来比较您的接口。

作为参考,丑陋的动态方法是:

let inline (=) a b =
  match a, b with
  | :? IFoo as a, :? IFoo as b -> yourEquals a b
  | _ -> a = b

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 = using inlin with op_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:

let inline (=) a b =
  match a, b with
  | :? IFoo as a, :? IFoo as b -> yourEquals a b
  | _ -> a = b
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文