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
varinfiere y sigue asignable;+=lo actualiza.finalata una vez cuando la línea corre —DateTime.now()vale.constdebe conocerse al compilar; la anotación reemplaza a var si hace falta.
Palabras clave y builtins usados aquí
constdoublefinalvarvoid
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.
Paso 2 de 3 en Valores; paso 2 de 27 en Fundamentos del lenguaje.