LEVIATHAN v962456e · 962456eee1

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> attributes

The attributes written on the method, with the values of their arguments.

attrs

Array<string> attrs

The names of the attributes written on the method.

name

string name

The method's name.

params

The method's parameters in order.

returnType

string returnType

The method's return type, spelled as the source wrote it; void for no result.

Methods

arity

arity() -> int

Count 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 | None

Find 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) -> bool

Check 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.