Declarations
late fields
Declare an instance field that gets its value after construction and raises if it is read before then.
since 0.1.0-alpha.1linuxwindowswasm
Description
A late field starts without a value and is given one later: in a constructor after this has
been used, in a method, or from another object. It needs no initializer and no assignment in the
constructor, so it suits a value that can only be made once the object exists — a delegate that is
handed this, a callback installed after construction, a result that arrives later.
class ListView {
late IRowDelegate delegate_; // the delegate is built from `this`
new ListView() { delegate_ = DefaultRowDelegate(this); }
}
Reading a late field before its first assignment raises a RuntimeException whose message names
the field: late field 'C.f' was read before it was assigned, C being the class that declares it.
A delegate built from this, and a read before the assignment
use io::console;
interface IRow { string render(int i); }
class View {
string name;
late IRow row;
new View(string n) { name = n; row = Plain(this); }
string show(int i) => row.render(i);
}
class Plain : IRow {
View owner;
new Plain(View v) { owner = v; }
string render(int i) => owner.name + "#" + i.toString();
}
class Lazy { late string label; }
console.writeln(View("list").show(3));
Lazy z = Lazy();
try {
console.writeln(z.label);
} catch (RuntimeException e) {
console.writeln(e.message);
}
z.label = "set";
console.writeln(z.label);
list#3
late field 'Lazy.label' was read before it was assigned
set
Rules
late T f;declares an instance field whose type is a non-optional reference type that is not a value struct: a class, an interface, a function type, a type parameter,string,Array,Map,Set,Block,Promise, and the like.- A
latefield starts unassigned. Any assignment gives it its value: in a constructor, in a method, or through another object. - Reading a
latefield before its first assignment raisesRuntimeExceptionwith the messagelate field 'C.f' was read before it was assigned, whereCis the class that declares the field. Every read is checked, also one through an interface or a type parameter. - The constructor rules for fields without a default (
lang.constructors) do not apply to alatefield: a constructor may usethisbefore assigning it, and need not assign it at all. lateis a compile error on astatic,const,readonlyorweakfield, on an optional type (T?already starts asNone), on a scalar, an enum or a value struct, together with an initializer, and on anything that is not a field.- A
latefield never default-constructs, so it is no link of a construction cycle (lang.fields):class Node { late Node next; }is accepted, and so isclass A { late B b; }withclass B { A a; }. lateis a contextual keyword: it is a modifier only at the start of a member; elsewhere it is an ordinary identifier (lang.keywords).
Examples
A callback installed after construction:
A late callback
use io::console;
class Button {
late (string) => void onClick;
void click() { onClick("ok"); }
}
Button b = Button();
b.onClick = (string s) => { console.writeln("clicked " + s); };
b.click();
clicked ok
See also
- Fields — Declare the data a class or struct holds, with optional initializers, and how a field without an initializer gets its default value.
- Constructors — Declare constructors with new, select among them by label and argument types, call base constructors, and construct classes that declare none.
- readonly fields — Fix an instance field once, either in its initializer or in every constructor, and keep it read-only afterwards.
- weak fields — Hold a non-owning reference to an object that reads as None once the object is gone.