Lexical
Keywords
The reserved words, the contextual words that are ordinary identifiers elsewhere, and the keywords that may still name members.
since 0.1.0-alpha.1linuxwindowswasm
Description
A keyword is a word the language reserves for its own syntax. These words cannot be used as the name of a variable, parameter, class or function:
namespace class struct enum interface public protected private new mutating
distinct const readonly static weak get set return var await
bind inject use uses if else while for in match is this true
false break continue do using try catch throw assert invariant
mutating marks a struct method that changes its receiver. The built-in collections' in-place verbs (add, addAll, insertAt, removeAt, clear on Array; set, remove, clear on Map; add, remove, clear on Set) are mutating members too, so they follow the same place rule: a verb needs a place for a receiver and cannot be taken as a bare method reference.
The built-in type names int, string, bool, float, char, byte, void and the other sized numeric types are not keywords. They are ordinary names that resolve to declarations in the standard library.
attribute, comptime, rule, macro, as, override and late are contextual keywords. They have a meaning only where a declaration, a use ... as ... import or (for override and late) a class member expects them. Everywhere else they are ordinary identifiers.
assert and invariant are reserved (lang.assert, lang.invariants). pre, post and old are contextual: pre( and post( directly after a parameter list or another contract clause start a contract clause, and old( is the capture form only inside a post clause (lang.contracts). Everywhere else they are ordinary identifiers.
Nine keywords can also be used as the name of a member: get, set, is, in, use, uses, new, bind and inject. A class may declare a field or method with one of those names, and object.name reaches it. Their keyword meaning applies only at the start of a statement or declaration.
Keywords as member names
use io::console;
class Switch {
bool is = true;
int in = 3;
int use() => 4;
string bind() => "bound";
int get() => 5;
}
Switch s = Switch();
console.writeln(s.is);
console.writeln(s.in);
console.writeln(s.use());
console.writeln(s.bind());
console.writeln(s.get());
true
3
4
bound
5
Rules
- A word in the keyword list cannot be a variable, parameter, class, function or namespace name.
assertandinvariantare in the list: using one as a name is'assert' is a reserved word(or'invariant'), and neither can follow.or::. - Of those,
get,set,is,in,use,uses,new,bindandinjectare accepted as the name of a field or method and after.. Every other keyword is rejected there. attribute,comptime,rule,macro,as,overrideandlateare ordinary identifiers outside their declaration positions, soint rule = 3;is legal.overrideandlateare modifiers only in member position, where a member can start after them (lang.override,lang.late);int override = 6;andint late = 7;are ordinary variables.pre,postandoldare ordinary identifiers outside contract positions, soint old = 1;is legal. A one-statement body that calls a function namedpreorpostmust be braced (void f() { pre(1); }), becausepre(after a parameter list starts a clause.int,string,bool,float,char,byteand the other primitive names are identifiers, not keywords.
Examples
Contextual keywords as ordinary names, and a keyword as a member name:
int attribute = 1;
int comptime = 2;
int rule = 3;
int macro = 4;
int as = 5;
int override = 6;
int late = 7;
int old = 1;
class Registry {
int get(int key) => key;
int set = 0;
int new = 1;
int override = 2;
}
Notes
readonly, static and weak are reserved even though they appear only in a few declarations.