错误fs1133:没有的构造函数适用于“列表<'a>”
-
14-11-2019 - |
题
我创建了一个类型的人,如下所示。
type Person =
{ First: string; Last : string }
override this.ToString() = sprintf "%s, %s" this.First this.Last
.
但是当我试图制作一个人的列表时,如下。
let people =
new List<_>(
[|
{First = "Bhushan"; Last = "Shinde"}
{ First = "Abhi"; Last = "Jad"}
|])
.
它给出了错误:没有构造函数适用于类型'列表<'a>'
可以请某人解释问题是什么?
谢谢。
解决方案
In F#, the name List<_>
is used to refer to the immutable F# list (defined in F# core library).
If you want to create mutable .NET list (System.Collections.Generic.List<_>
), you need to use an alias that is defined in the F# library ResizeArray<_>
(or you need to use fully qualified name):
let people =
new ResizeArray<_>(
[|
{First = "Bhushan"; Last = "Shinde"}
{ First = "Abhi"; Last = "Jad"}
|])
If you want to create a normal F# list (and use it in a functional style), then you can just use the list comprehension syntax without passing the value to any constructor:
let people =
[ {First = "Bhushan"; Last = "Shinde"}
{ First = "Abhi"; Last = "Jad"} ]
不隶属于 StackOverflow