Records en F#
Paquetes con nombre inmutables, con igualdad estructural incluida.
// Records are immutable named bundles; with copies and updates.
type Hive = { Name: string; Frames: int; QueenYear: int }
let willow = { Name = "Willow"; Frames = 8; QueenYear = 2024 }
printfn "%A" willow
// The with expression copies, changing only what you name.
let grown = { willow with Frames = 10 }
printfn "%s grew to %d frames" grown.Name grown.Frames
printfn "Original untouched: %d" willow.Frames
// Structural equality comes free.
printfn "Same hive? %b" (willow = { grown with Frames = 8 })
Cómo funciona
type Hive = { ... }declara la forma; construir nombra campos.{ willow with Frames = 10 }copia cambiando un campo.=compara por valor, no por referencia.
Palabras clave y builtins usados aquí
letstringtypewith
El intento, en números
- Líneas
- 13
- Caracteres a escribir
- 506
- Tokens
- 84
- Ritmo de tres estrellas
- 70 tpm
Al ritmo de tres estrellas de 70 tokens por minuto, este intento toma unos 72 segundos.
Paso 1 de 3 en Tipos; paso 13 de 27 en Fundamentos del lenguaje.