创建通用列表< T>在F#中 [英] Creating a generic List <T> in F#

查看:93
本文介绍了创建通用列表< 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.

这篇关于创建通用列表&lt; T&gt;在F#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆