LEVIATHAN vd52e79c4 · d52e79c486

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

use io::console;

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 instance fields in declaration order (its static fields are in staticFields).

invariants

Array<meta::Contract> invariants

The class's own invariants in written order (not inherited ones).

methods

The class's instance methods in declaration order (its static ones are in staticMethods).

name

string name

The class's name.

staticFields

Array<meta::Field> staticFields

The class's static fields in declaration order.

staticMethods

Array<meta::Method> staticMethods

The class's static methods and static accessors in declaration order.

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

use io::console;

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.