Errore FS1133: nessun costruttore è disponibile per il tipo 'Elenco <' A> '
-
14-11-2019 - |
Domanda
Ho creato una persona di tipo come segue che funziona bene.
type Person =
{ First: string; Last : string }
override this.ToString() = sprintf "%s, %s" this.First this.Last
.
Ma quando provo a fare un elenco di persona come segue.
let people =
new List<_>(
[|
{First = "Bhushan"; Last = "Shinde"}
{ First = "Abhi"; Last = "Jad"}
|])
.
Dà l'errore: Nessun costruttore è disponibile per l'elenco del tipo <'A>'
Qualcuno può spiegare qual è il problema?
Grazie.
Soluzione
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"} ]