创建通用列表< T>在F#中

创建通用列表< T>在F#中

问题描述:

我正在尝试像这样在F#中创建一个标准.NET List<T>:

I am trying to create a standard .NET List<T> in F# like this:

module Csv

open System;

type Sheet () =
  let rows = new List<Object>()

但出现以下错误:

对于List<Object>
类型没有可用的构造函数 C:\…\Csv.fs:6

No constructors are available for the type List<Object>
C:\…\Csv.fs: 6

我在做什么错了?

作为其他建议的更简单替代方案,您可以使用名为ResizeArray<T>的类型.这是F#核心库中定义的System.Collections.Generic.List<T>的类型别名:

As a simpler alternative to what others suggest, you can use the type named ResizeArray<T>. This is a type alias for System.Collections.Generic.List<T> defined in the F# core libraries:

type Sheet () = 
  let rows = new ResizeArray<Object>() 

在已编译的代码中,ResizeArray<T>将被编译为System.Collections.Generic. List<T>,因此,如果您使用C#中的库,则不会有任何区别.

In the compiled code, ResizeArray<T> will be compiled down to System.Collections.Generic. List<T>, so if you use your library from C#, there will not be any difference.

您不必打开System.Collections.Generic,这会隐藏F#List<T>类型的定义(尽管这不是大问题),我认为ResizeArray是数据的更合适的名称无论如何,结构.

You do not have to open System.Collections.Generic, which would hide the definition of the F# List<T> type (though this is not a big problem), and I think that ResizeArray is a more appropriate name for the data structure anyway.