Pregunta

He creado una persona de tipo de la siguiente manera que se ejecuta bien.

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

Pero cuando intento hacer una lista de la persona de la siguiente manera.

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

Da el error: No hay constructores disponibles para el tipo 'Lista <' a> '

¿Puede alguien explicar cuál es el problema?

gracias.

¿Fue útil?

Solución

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"} ]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top