Standard Library · meta
class Method
A method of a class, as a rule sees it.
since 0.1.0-alpha.1linuxwindowswasm
Examples
attribute Inspect { }
rule inspect {
match @Inspect(i) on class C
inject `Array<string> signatures() => [ $for s in
C.methods.map((m) => m.name + "/" + m.arity().toString() + " -> " + m.returnType) : $s ];` at member of C
}
@Inspect
class Calc {
int add(int a, int b) => a + b;
string label() => "calc";
void reset() { }
}
console.writeln(Calc().signatures().joinToString("; "));
add/2 -> int; label/0 -> string; reset/0 -> void
Fields
attributes
Array<meta::Attr> attributesThe attributes written on the method, with the values of their arguments.
attrs
Array<string> attrsThe names of the attributes written on the method.
name
string nameThe method's name.
params
Array<meta::Param> paramsThe method's parameters in order.
returnType
string returnTypeThe method's return type, spelled as the source wrote it; void for no result.
Methods
arity
arity() -> intCount the method's parameters.
Returns
The number of parameters.
Examples
attribute Inspect { }
rule inspect {
match @Inspect(i) on class C
inject `Array<string> arities() => [ $for s in C.methods.map((m) => m.name + "=" + m.arity().toString()) : $s ];` at member of C
}
@Inspect
class Calc {
int add(int a, int b) => a + b;
int negate(int a) => 0 - a;
void reset() { }
}
console.writeln(Calc().arities().joinToString(","));
add=2,negate=1,reset=0
attr
attr(string n) -> meta::Attr | NoneFind an attribute on the method by name, with its argument values.
Parameters
- n
- The attribute name, without the
@.
Returns
The attribute, or None when the method does not carry it.
Examples
attribute Route { string path = ""; }
attribute Inspect { }
rule inspect {
match @Inspect(i) on class C
inject `Array<string> routes() => [ $for s in C.methods.where((m) => m.hasAttr("Route")).map((m) =>
m.name + " at " + (m.attr("Route")?.argStr(0) ?? "/")) : $s ];` at member of C
}
@Inspect
class Api {
@Route("/users") void users() { }
@Route void home() { }
void helper() { }
}
console.writeln(Api().routes().joinToString(","));
users at /users,home at /
See also: Attr
hasAttr
hasAttr(string n) -> boolCheck whether the method carries an attribute of a given name.
Parameters
- n
- The attribute name, without the
@.
Returns
true when the method has that attribute.
Examples
attribute Audit { }
attribute Inspect { }
rule inspect {
match @Inspect(i) on class C
inject `Array<string> audited() => [ $for s in C.methods.where((m) => m.hasAttr("Audit")).map((m) => m.name) : $s ];` at member of C
}
@Inspect
class Store {
@Audit void save() { }
void load() { }
@Audit void remove() { }
}
console.writeln(Store().audited().joinToString(","));
save,remove
See also
- Class — A class that a rule has matched, as a rule sees it.