typestar

let y var en Swift

Constantes por defecto, mutación a pedido, tipos por inferencia.

// let binds a constant; var stays mutable. Types mostly infer.
let boat = "Kestrel"
var crew = 3
crew += 1

// Annotate when inference would guess wrong.
let handicap: Double = 0.92
let legNm = 4.5
print("\(boat) sails with \(crew) aboard")
print("Corrected leg: \(legNm * handicap) nm")

// Constants of any type, computed once at startup.
let courseMarks = ["pin", "windward", "leeward"]
print("Course has \(courseMarks.count) marks")

Cómo funciona

  1. let boat no admite reasignación; var crew sí, y += lo actualiza.
  2. handicap: Double guía la inferencia donde adivinaría Int.
  3. Las colecciones también infieren: courseMarks es un [String].

Palabras clave y builtins usados aquí

El intento, en números

Líneas
14
Caracteres a escribir
437
Tokens
82
Ritmo de tres estrellas
70 tpm

Al ritmo de tres estrellas de 70 tokens por minuto, este intento toma unos 70 segundos.

Escribe este fragmento

Paso 2 de 3 en Valores; paso 2 de 27 en Fundamentos del lenguaje.

← Anterior Siguiente →