typestar

var, final y const en Dart

Tres formas de atar: mutable, una vez en runtime, fija al compilar.

void main() {
  // var infers and stays mutable; final binds once at run time;
  // const is fixed at compile time.
  var sightings = 12;
  sightings += 1;
  final opened = DateTime.now();
  const perch = 'north fence';

  // A type annotation replaces var when inference needs steering.
  double meanPerHour = sightings / 4;
  print('$sightings from the $perch since $opened');
  print('Rate: ${meanPerHour.toStringAsFixed(1)} per hour');
}

Cómo funciona

  1. var infiere y sigue asignable; += lo actualiza.
  2. final ata una vez cuando la línea corre — DateTime.now() vale.
  3. const debe conocerse al compilar; la anotación reemplaza a var si hace falta.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
13
Caracteres a escribir
421
Tokens
72
Ritmo de tres estrellas
70 tpm

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

Escribe este fragmento

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

← Anterior Siguiente →