表格列出哪些F#类型是值类型以及哪些是引用类型的参考

时间:2021-02-19 16:10:34

Is there a good reference that tabulates which F# types are value types and which are reference types? Alternatively, is there a good way to query a type to determine which it is? Yes, I know that things that are classes are reference types and things that are structs are value types. At the same time, this distinction isn't always intuitive to me with F# types. For example, tuple types are reference types. Evidently, so are record types.

是否有一个很好的参考,列出哪些F#类型是值类型,哪些是引用类型?或者,是否有一种查询类型以确定它的好方法?是的,我知道类的东西是引用类型,结构的东西是值类型。同时,对于F#类型,这种区别并不总是直观的。例如,元组类型是引用类型。显然,记录类型也是如此。

1 个解决方案

#1


4  

You can always check whether something is a value type using F# Interactive:

您始终可以使用F#Interactive检查某些内容是否为值类型:

typeof<int * int>.IsValueType

As you mentioned, tuples are reference types and so this returns false.

正如您所提到的,元组是引用类型,因此返回false。

I agree that the difference is not always clear. In general, most types that you work with in F# are reference types (and all F# types that you define with the exception of struct types).

我同意差异并不总是很清楚。通常,您在F#中使用的大多数类型都是引用类型(以及您定义的所有F#类型,但结构类型除外)。

  • Primitive types (numerical and booleans) are value types
  • 原始类型(数字和布尔值)是值类型
  • But string is a reference type
  • 但字符串是引用类型
  • There are a couple of struct types defined in .NET like DateTime and TimeSpan and also KeyValuePair (this is sometimes confusing as this is quite similar to tuple).
  • .NET中定义了几种结构类型,如DateTime和TimeSpan以及K​​eyValuePair(这有时令人困惑,因为它与元组非常相似)。
  • F# tuples, records, discriminated unions, lists, arrays are all reference types
  • F#元组,记录,区分联合,列表,数组都是引用类型
  • F# object types are also reference types unless they are marked as Struct.
  • F#对象类型也是引用类型,除非它们标记为Struct。

#1


4  

You can always check whether something is a value type using F# Interactive:

您始终可以使用F#Interactive检查某些内容是否为值类型:

typeof<int * int>.IsValueType

As you mentioned, tuples are reference types and so this returns false.

正如您所提到的,元组是引用类型,因此返回false。

I agree that the difference is not always clear. In general, most types that you work with in F# are reference types (and all F# types that you define with the exception of struct types).

我同意差异并不总是很清楚。通常,您在F#中使用的大多数类型都是引用类型(以及您定义的所有F#类型,但结构类型除外)。

  • Primitive types (numerical and booleans) are value types
  • 原始类型(数字和布尔值)是值类型
  • But string is a reference type
  • 但字符串是引用类型
  • There are a couple of struct types defined in .NET like DateTime and TimeSpan and also KeyValuePair (this is sometimes confusing as this is quite similar to tuple).
  • .NET中定义了几种结构类型,如DateTime和TimeSpan以及K​​eyValuePair(这有时令人困惑,因为它与元组非常相似)。
  • F# tuples, records, discriminated unions, lists, arrays are all reference types
  • F#元组,记录,区分联合,列表,数组都是引用类型
  • F# object types are also reference types unless they are marked as Struct.
  • F#对象类型也是引用类型,除非它们标记为Struct。