Tub*_*Tub 6 f# structural-equality recordtype
如果我有这样的记录:
type MyDate =
{ Year : int
Month : int
Day : int }
Run Code Online (Sandbox Code Playgroud)
我知道 F# 的结构比较将确保在对列表进行排序时,它将保持一致的顺序。
我的问题是我是否可以依靠它以特定方式进行比较,这种方式是什么?
例如MyDate上面的记录:如果它按照声明的顺序比较每个字段,那么我可以假设以下内容:
{ Year: 2010; Month: 9: Day: 8 } > { Year: 2009; Month: 10; Day: 20 }
我一直在努力寻找描述 Record 类型的结构相等性如何工作的文档。我可以从 Fsharp.Core 测试中看到元组比较是如何工作的:https : //github.com/fsharp/fsharp/blob/cb6cb5c410f537c81cf26825657ef3bb29a7e952/tests/fsharp/core/attributes/test.fsx , but I can't'为 Record 类型找到类似的测试。
啊,我发布问题后立即找到了答案:
\n\n来自语言规范:https://fsharp.org/specs/language-spec/4.1/FSharpSpec-4.1-latest.pdf
\n\n\n\n\n8.15.4 生成的 CompareTo 实现的行为
\n\n...
\n\n\xe2\x80\xa2 如果 T 是结构或记录类型,则按声明顺序对 x 和 y 的每个对应字段对调用 FSharp.Core.Operators.compare,并返回第一个非零结果。
\n
因此,在上面的MyDate示例中,当比较两个实例时,它将:
Year首先比较字段Month字段Day字段