Question

J'ai créé une personne de type comme suit qui fonctionne bien.

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

Mais lorsque j'essaie de faire une liste de personnes comme suit.

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

Il donne l'erreur: aucun constructeur n'est disponible pour la liste de type 'A>'

Quelqu'un peut-il s'il vous plaît expliquer quel est le problème?

merci.

Était-ce utile?

La solution

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"} ]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top