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 private new mutating
distinct const readonly weak get set return var let await bind
inject use uses if else while for in match is this true false
break continue do using try catch throw
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 and override are contextual keywords. They have a meaning only where a declaration, a use ... as ... import or (for override) a class member expects them. 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
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.
- 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,asandoverrideare ordinary identifiers outside their declaration positions, soint rule = 3;is legal.overrideis a modifier only in member position, where a member can start after it (lang.override);int override = 6;is an ordinary variable.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;
class Registry {
int get(int key) => key;
int set = 0;
int new = 1;
int override = 2;
}
Notes
readonly and weak are reserved even though they appear only in a few declarations.