Standard Library · meta
class Class
A class that a rule has matched, as a rule sees it.
since 0.1.0-alpha.1linuxwindowswasm
Overview
A rule binds the matched class to a name and reads its name, bases, fields and
methods to decide what code to generate.
Examples
attribute Inspect { }
rule inspect {
match @Inspect(i) on class C
inject `string summary() => $C.name + ": " +
[ $for s in C.fields.map((f) => f.name) : $s ].joinToString("+") + " / " +
[ $for s in C.methods.map((m) => m.name) : $s ].joinToString("+");` at member of C
}
@Inspect
class Counter {
int count;
string label;
void bump() { }
int read() => count;
}
console.writeln(Counter().summary());
Counter: count+label / bump+read
Fields
bases
Array<string> basesThe class's base types, spelled as the source wrote them.
fields
Array<meta::Field> fieldsThe class's fields in declaration order.
methods
Array<meta::Method> methodsThe class's methods in declaration order.
name
string nameThe class's name.
Methods
hasBase
hasBase(string n) -> boolCheck whether the class lists a given base type.
Parameters
- n
- The base type's name, spelled as the class wrote it.
Returns
true when n is one of the class's bases.
Examples
attribute Inspect { }
interface Shape { }
rule markShapes {
match @Inspect(i) on class C
where C.hasBase("Shape")
inject `string kind() => "a shape";` at member of C
}
@Inspect
class Circle : Shape {
int radius;
}
@Inspect
class Plain {
int n;
string kind() => "not a shape";
}
console.writeln(Circle().kind());
console.writeln(Plain().kind());
a shape
not a shape