LEVIATHAN v962456e · 962456eee1

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

The class's base types, spelled as the source wrote them.

fields

The class's fields in declaration order.

methods

The class's methods in declaration order.

name

string name

The class's name.

Methods

hasBase

hasBase(string n) -> bool

Check 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

See also

  • Field — A field of a class, as a rule sees it.
  • Method — A method of a class, as a rule sees it.