F#记录与.net结构

use*_*321 11 .net f#

f#记录是否与.net结构相同?我看到人们谈论f#struct,他们是否使用这个术语与F#记录可互换?就像在FSharp中运行我的算法比Python慢,谈论使用struct作为字典键但是使用代码type Tup = {x: int; y: int}为什么这比上面的链接中的字典键更快?

Ste*_*sen 16

不,实际上,F#中的记录类型是一种引用类型,只是具有特殊的函数编程功能,如属性上的模式匹配,更容易的不变性和更好的类型推断.

我认为Laurent的加速一定是出于其他原因,因为我们可以证明这Tup不是ValueType:

type Tup = {x: int; y: int}
typeof<Tup>.BaseType = typeof<System.Object> //true
Run Code Online (Sandbox Code Playgroud)

type StructType = struct end
typeof<StructType>.BaseType = typeof<System.ValueType> //true
typeof<StructType>.BaseType = typeof<System.Object> //false
Run Code Online (Sandbox Code Playgroud)

  • 找到这些问题答案的方法是使用反射器在C#中反汇编你的F#程序集,看看F#编译器发出了什么代码 (2认同)