Repository navigation
v2.34.0 - Records and top-level variables (lexical scope)
Two gaps from doc/dart_language_gaps.md:
top-level variables, and records.
Top-level variables and lexical scope
Names now resolve lexically: a function or method sees the module it is
declared in, not its caller.
int count = 0;
int next() { count++; return count; }
int run() {
var count = 100; // a local: `next()` still uses the top-level `count`
next();
return next(); // 2
}- A function no longer sees its caller's locals, or its caller's
this.
Before, a call resolved names through the caller's scope (dynamic scoping).
So a caller's local shadowed a top-level variable inside the callee, and a
function could read a variable that only its caller declared. - A class-method entry point sees the top-level variables.
executeClassMethodfailed withCan't find variablewhen a member read
one. - Imported top-level variables:
import 'lib.dart';makeslib's
variables readable and writable. An imported function or class uses its
own module's variables. An imported module runs its variable initializers
on first use. - Generated code declares top-level variables before the functions. This
makes generated Lua valid: alocaldeclared after the functions that use
it is invisible to them. - Python output declares
global xin a function that assigns a
module-level variable, which Python requires. The Python parser reads
globalback. - Initialization is eager and in order: a variable whose initializer reads a
variable declared after it fails, where Dart initializes lazily.
Records
(int, {String name}) make(int n) => (n, name: 'r$n');
var r = make(3);
print('${r.$1} ${r.name}'); // 3 r3
print((1, x: 2) == (1, x: 2)); // true- Literals:
(1, 'a'),(x: 1, y: 2), positional and named fields
interleaved, and(1,). Fields are evaluated in source order. - Types:
(int, String),({int x, int y}),(int, {String name}),
(int,), nullable(int, int)?, and inside collection types
(List<(int, int)>,<(int, int)>{},<String, (int, int)>{}). A record type accepts a record of the same shape whose
fields it accepts, so(num, num)accepts(1, 2.5). - Field access:
r.$1,r.name. A missing field is a runtime error. - Equality and
toStringare Dart's: structural==andhashCode, so
records work as set elements and map keys, and'$r'renders as
(1, a, x: 2). is:r is (int, int)tests the shape and the field types.- A record reaches the host as a
VMRecord. - Records survive the binary AST (node tag 130, type-table kind
0x18) and
are generated as Dart. The other targets, and Wasm, refuse them. The
rest of Dart 3's record features need patterns, which aren't supported yet:
destructuring, andswitchon records.
Other fixes
return nullfrom a nullable function returned the wrong value. From a
String?function or getter it gavevoid, and from aList<int>?
function a non-null value. It now returnsnull.var x = C().m()was typed asC(the receiver), so the first
assignment failed. It now has the call's return type.var x = f([a, 2])failed whenftakes aList<int>andahas no
static type, because inferringxmatchedList<dynamic>against
List<int>strictly. An argument whose type arguments are unknown now
matches; the call is still checked against the actual values when it runs.l[0].fielddidn't parse, and a field read off an instance kept in a
collection failed. Both work now, includingpairs[i].$2.