Declarations
Accessors
Replace how a field is read or written with get and set, and define computed values and indexers.
since 0.1.0-alpha.1linuxwindowswasm
Description
An accessor changes what reading or writing a name does. get defines a read, set a write:
get value() => value; // read view over the 'value' slot (its type is the field's)
set value(int v) value = v * 2; // write view
int get ([])(int i) => cells[i]; // indexer (computed accessor, declares its type)
set ([])(int i, int v) cells[i] = v; // the value being stored is the last parameter
A parameterless accessor is a view over a field of the same name. Inside the accessor body the
name refers to the raw field, so get value() => value + 100 reads the stored number and does not
call itself. A get can also be declared with no field of that name, which makes it a computed,
read-only property; a computed get declares its type before get, as a method does:
int get doubled() => value * 2;.
An accessor with parameters is a computed accessor and needs no backing field. The index accessor
([]) is the common case: a[i] calls the get ([]) accessor of a's class and a[i] = v calls
the set ([]) accessor, which mutates the object in place.
Views, computed properties and an indexer
use io::console;
class Gauge {
int value = 1;
get value() => value + 100;
set value(int v) value = v * 2;
int get doubled() => value * 2;
}
Gauge g = Gauge();
console.writeln(g.value);
g.value = 5;
console.writeln(g.value);
console.writeln(g.doubled);
class Grid {
Array<int> cells;
new Grid() { cells = [0, 0, 0, 0]; }
int get ([])(int i) => cells[i];
set ([])(int i, int v) cells[i] = v;
}
Grid grid = Grid();
grid[2] = 7;
grid[0] += 5;
console.writeln(grid[2]);
console.writeln(grid[0]);
console.writeln(grid[1]);
101
110
220
7
5
0
Setting g.value = 5 stores 10 in the field, because the set accessor doubles it; reading it
back adds 100. g.doubled is computed from value through its accessor, so it is (10 + 100) * 2.
Rules
get name() => exprandset name(T v) statementdefine the read and the write ofname.- A read through a
gethas the accessor's type. A view (a parameterlessgetover a same-named field) has the field's type and may write it:int get value() => value + 100;over anint value. Any otherget(a computed property or an indexer) must write its type, and a view that writes one must write the field's:
error: a computed accessor declares its type: write it first, as in 'int get doubled() => …'
error: an indexer declares its type: write it first, as in 'int get ([])(int i) => …'
error: the accessor 'get value' views the field 'value : int'; its type must be 'int'
int get(int x), withgetfollowed by a parameter list, is an ordinary method namedget.s[i]on astringreads thecharat byte offseti(the string'sget ([]), which iss.at(i)).- A
getbody must return a value on every path, as a function's must (lang.statements); a block body that can reach its end, or a barereturn;in it, is a compile error. - Inside an accessor for
name, the bare namenameandthis.nameare the raw field: the accessor does not call itself. Other names go through their own accessors. - A parameterized accessor is computed and needs no backing field. The value being assigned is the
last parameter of a
set. - Indexers overload by index type, across a class and its bases too:
a[i]calls the indexer ofi's type, whichever class in the hierarchy declares it. An indexer that has such an overload and is overridden below the receiver's static type is a compile error at the use. Indexers are the one exception tolang.method-dispatch: a method or an operator dispatches on the overload the static types chose, but an indexer is found at run time by its name, so the run-time dispatch cannot tell the overloads apart. - A
getorsetan interface declares without a body is a requirement. A data field of the same name and type satisfies it. A class's body-lessgetorsetis the same: where the object's class holds a data field of that name (class A { set p(P v); },class C : A { P p = P(1); }), reads and writes reach the field, and a change through it (c.p.x = 5,c.p.bump()) lands in the field. - The index accessors are written
T get ([])(params)andset ([])(params, value); a space or a comment inside the selector's parentheses is a compile error.a[i] += vreads throughgetand writes throughset. - If a name has only a
getaccessor, writes go to the field directly (the stored value is changed but later reads still pass throughget). If agethas no field behind it, a write to the name is silently ignored. - If a name has only a
setaccessor, reads return the field directly. - Accessors may be
static(lang.static-members); their write rule differs: a write to a get-only static property without a backing static field is a compile error. - An accessor is bound to the field of its name in the class that declares it: in a combined
object, a same-named field that another base contributes (a
distinctone, say) is read and written directly, never through this accessor. - A
setaccessor in a struct is mutating without being marked. Asetaccessor may not be declared over aconstorreadonlyfield.
Examples
A write to a name that has only a computed get is ignored:
A get without a field
use io::console;
class C {
int stored = 4;
int get twice() => stored * 2;
}
C c = C();
c.twice = 3;
console.writeln(c.twice);
c.stored = 10;
console.writeln(c.twice);
8
20
An accessor on a struct:
A set accessor on a struct
use io::console;
struct Temp {
int raw;
int get celsius() => raw;
set celsius(int v) raw = v * 2;
}
Temp t = Temp(1);
t.celsius = 5;
console.writeln(t.raw);
10
See also
- Operator declarations — Define operators on a class or struct by declaring a member with a symbolic selector such as (+), (==) or ([]).
- Fields — Declare the data a class or struct holds, with optional initializers, and how a field without an initializer gets its default value.
- Value structs — Declare value types that are copied on every assignment, pass and return, with derived equality and explicit mutating methods.