F# 在私有联合类型上公开比较/相等
我正努力在一个案例被隐藏的联合类型上公开比较/相等。这是我开始的: 模块.FSIF# 在私有联合类型上公开比较/相等,f#,F#,我正努力在一个案例被隐藏的联合类型上公开比较/相等。这是我开始的: 模块.FSI type A<'T when 'T : comparison> [<CustomEquality;CustomComparison>] type A<'T when 'T : comparison> with interface System.IComparable override Equals : y:obj -> bool overrid
type A<'T when 'T : comparison>
[<CustomEquality;CustomComparison>]
type A<'T when 'T : comparison>
with
interface System.IComparable
override Equals : y:obj -> bool
override GetHashCode : unit -> int
end
这是公开从联合类型到私有案例的比较的正确方法吗?签名文件中的正确语法是什么?错误消息仅说明FSI文件声明的正确语法应使用
=
(如在实现文件中)而不是带有。。。结束语法。然而,这并不能真正解决问题。经过一些实验后,我认为您不需要应用FSI文件中的属性:
// Mod.fs
namespace Mod
[<CustomEquality; CustomComparison>] //; CustomEquality>]
type A<'T when 'T : comparison> =
| A of 'T list
override x.Equals y = compare x (y :?> A<_>) = 0
override x.GetHashCode() = -1
interface System.IComparable with
member x.CompareTo(y) = compare x (y :?> A<_>)
// Mod.fsi
namespace Mod
[<Sealed>]
type A<'T when 'T : comparison> =
interface System.IComparable
override Equals : obj -> bool
override GetHashCode : unit -> int
明亮的添加[]并删除其他属性修复了该问题。我试过和他们一起玩,但显然从来没有找到正确的组合。对我来说,需要添加Sealed看起来像一个bug(或者至少是一个不幸的不一致性)。联合是自动密封的(如果我也在FS文件中添加sealed,编译器会这么说)。@petebu-sealed属性是故意的(编译器只是想让您在F#接口文件中显式声明这一事实-尽管当接口文件包含类型声明时不需要它)。
The struct, record or union type 'B' has the 'StructuralComparison'
attribute but the component type 'Module.A<char>' does not satisfy the
'comparison' constraint
[<CustomEquality;CustomComparison>]
type A<'T when 'T : comparison>
with
interface System.IComparable
override Equals : y:obj -> bool
override GetHashCode : unit -> int
end
[<CustomEquality;CustomComparison>]
type A<'T when 'T : comparison> = A of 'T list
with
override x.Equals y = ...
override x.GetHashCode() = ...
interface System.IComparable with
member x.CompareTo(y) = ...
This construct is deprecated: The syntax 'type X with ...' is reserved
for augmentations. Types whose representations are hidden but which have
members are now declared in signatures using 'type X = ...'.
// Mod.fs
namespace Mod
[<CustomEquality; CustomComparison>] //; CustomEquality>]
type A<'T when 'T : comparison> =
| A of 'T list
override x.Equals y = compare x (y :?> A<_>) = 0
override x.GetHashCode() = -1
interface System.IComparable with
member x.CompareTo(y) = compare x (y :?> A<_>)
// Mod.fsi
namespace Mod
[<Sealed>]
type A<'T when 'T : comparison> =
interface System.IComparable
override Equals : obj -> bool
override GetHashCode : unit -> int
// Mod.fsi
namespace Mod
type A<'T when 'T : comparison> =
private | A of 'T list
// Mod.fs
namespace Mod
[<StructuralComparisonAttribute; StructuralEqualityAttribute>]
type A<'T when 'T : comparison> =
| A of 'T list