f# - 错误 FS1133 : No constructors are available for

我创建了一个运行良好的 Person 类型。

type Person = 
    { First: string; Last : string }
    override this.ToString() = sprintf "%s, %s" this.First this.Last

但是当我尝试如下列出 Person 时。

let people = 
    new List<_>( 
        [|
            {First = "Bhushan"; Last = "Shinde"} 
            { First = "Abhi"; Last = "Jad"}
        |])

它给出错误:No constructors are available for the type 'List'

谁能解释一下这是什么问题?

谢谢。

最佳答案

在 F# 中,名称 List<_>用于引用不可变的 F# 列表(在 F# 核心库中定义)。

如果要创建可变 .NET 列表 (System.Collections.Generic.List<_>),则需要使用 F# 库中定义的别名 ResizeArray<_> (或者您需要使用完全限定名称):

let people =  
    new ResizeArray<_>(  
        [| 
            {First = "Bhushan"; Last = "Shinde"}  
            { First = "Abhi"; Last = "Jad"} 
        |]) 

如果你想创建一个普通的 F# 列表(并以函数式风格使用它),那么你可以只使用列表理解语法而不将值传递给任何构造函数:

let people =  
        [ {First = "Bhushan"; Last = "Shinde"}  
          { First = "Abhi"; Last = "Jad"} ]

关于f# - 错误 FS1133 : No constructors are available for the type 'List<' a>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9513891/

相关文章:

ajax - JSF f :ajax listener not called

cuda - nVIDIA CUDA 驱动程序究竟做了什么?

django - 如何在 Django 开发中提供静态文件

python - 如何设置浏览器为 webbrowser 打开 localhost?

ruby-on-rails - 创建 Rails 音乐播放器应用程序(类似于 Rdio)

oracle - 如何以编程方式在 PL/SQL 中设置表名?

optimization - 为什么在没有 mem2reg 的情况下优化过程不起作用?

css - 我怎样才能将CSS选择器组合在一起

html - onclick按钮边框颜色改变

asp.net - 如何检查 Amazon S3 存储桶中文件夹的子文件夹大小