Pergunta

Criei um tipo Person da seguinte forma, que funciona bem.

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

Mas quando tento fazer uma lista de Person da seguinte maneira.

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

Dá o erro: Nenhum construtor está disponível para o tipo 'List<'a>'

Alguém pode explicar qual é o problema?

Obrigado.

Foi útil?

Solução

Em F#, o nome List<_> é usado para se referir à lista imutável do F# (definida na biblioteca principal do F#).

Se você deseja criar uma lista .NET mutável (System.Collections.Generic.List<_>), você precisará usar um alias definido na biblioteca F# ResizeArray<_> (ou você precisa usar um nome totalmente qualificado):

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

Se você deseja criar uma lista F# normal (e usá-la em um estilo funcional), basta usar a sintaxe de compreensão de lista sem passar o valor para nenhum construtor:

let people =  
        [ {First = "Bhushan"; Last = "Shinde"}  
          { First = "Abhi"; Last = "Jad"} ]
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top