首页 > 解决方案 > 警告 FS1178“不支持结构平等”

问题描述

我尝试在大型代码库上启用 5 级警告,它主要触发了数百个:

警告 FS1178 结构、记录或联合类型“Foo”不支持结构相等,因为类型“(单元 -> IBar 选项)”不满足“平等”约束。考虑将 'NoEquality' 属性添加到类型 'Foo' 以阐明该类型不支持结构相等

我们的许多类型引用 C# 类型,或包含函数类型(如上)。NoComparison根据需要遍历所有这些和/NoEquality属性会给我们带来什么好处?

标签: f#

解决方案


如果您尝试比较Foo代码中其他地方的实例,[<NoComparison>][<NoEquality>]on的存在Foo将使后续代码生成的编译器错误更容易理解。

没有属性的例子:

type Foo =
    {
        Func : int -> int
    }

let problem = Set<Foo>

(*
Compiler error:
The type 'Foo' does not support the 'comparison' constraint
because it is a record, union or struct with one or more
structural element types which do not support the
'comparison' constraint. Either avoid the use of comparison
with this type, or add the 'StructuralComparison' attribute
to the type to determine which field type does not support
comparison
*)

带有属性的示例:

[<NoComparison>]
type Foo =
    {
        Func : int -> int
    }

let problem = Set<Foo>

(*
Simpler compiler error:
The type 'Foo' does not support the 'comparison' constraint
because it has the 'NoComparison' attribute
*)

推荐阅读