LEVIATHAN v962456e · 962456eee1

Standard Library · meta

class Attr

An attribute written on a field or method, with the values of its arguments.

since 0.1.0-alpha.1linuxwindowswasm

Overview

Besides its name, an attribute carries its arguments in the order the attribute declares its fields. A rule can read them to configure what it generates, for example a column name taken from @Column("full_name").

Examples

attribute Column { string col = ""; int width = 0; }
attribute Inspect { }

rule inspect {
    match @Inspect(i) on class C
    inject `Array<string> columns() => [ $for s in C.fields.where((f) => f.hasAttr("Column")).map((f) =>
        f.name + " -> " + f.attr("Column")?.name + "(" + f.attr("Column")?.argCount().toString() + " args)") : $s ];` at member of C
}

@Inspect
class User {
    @Column("full_name", 40) string name;
    @Column int id;
    int cache;
}

console.writeln(User().columns().joinToString("; "));
name -> Column(2 args); id -> Column(2 args)

Fields

args

The argument values, one per field the attribute declares, in declaration order.

name

string name

The attribute's name without the @, for example Column.

Methods

argBool

argBool(int i) -> bool | None

Read an argument as a boolean.

An argument that was not written at the use site reads as None.

Parameters

i
The zero-based position of the argument.

Returns

The argument's boolean value, or None when it was left out.

Examples

attribute Column { string col = ""; bool key = false; }
attribute Inspect { }

rule inspect {
    match @Inspect(i) on class C
    inject `Array<string> keys() => [ $for s in C.fields.where((f) => f.hasAttr("Column")).map((f) =>
        f.name + "=" + (f.attr("Column")?.argBool(1) ?? false).toString()) : $s ];` at member of C
}

@Inspect
class User {
    @Column("id", true) int id;
    @Column("name") string name;
}

console.writeln(User().keys().joinToString(","));
id=true,name=false

See also: argStr

argCount

argCount() -> int

Count the arguments, which is the number of fields the attribute declares.

Arguments left out at the use site still count; they report present as false.

Returns

The number of argument slots.

Examples

attribute Bounds { int lo = 0; int hi = 0; }
attribute Inspect { }

rule inspect {
    match @Inspect(i) on class C
    inject `Array<string> counts() => [ $for s in C.fields.map((f) =>
        f.name + "=" + f.attr("Bounds")?.argCount().toString()) : $s ];` at member of C
}

@Inspect
class Gauge {
    @Bounds(1, 9) int level;
}

console.writeln(Gauge().counts().joinToString(","));
level=2

argFloat

argFloat(int i) -> float | None

Read an argument as a float.

An argument that was not written at the use site reads as None.

Parameters

i
The zero-based position of the argument.

Returns

The argument's float value, or None when it was left out.

Examples

attribute Scale { float factor = 1.0; }
attribute Inspect { }

rule inspect {
    match @Inspect(i) on class C
    inject `Array<string> factors() => [ $for s in C.fields.where((f) => f.hasAttr("Scale")).map((f) =>
        f.name + "=" + (f.attr("Scale")?.argFloat(0) ?? 1.0).toString()) : $s ];` at member of C
}

@Inspect
class Sensor {
    @Scale(2.5) float reading;
    @Scale float offset;
}

console.writeln(Sensor().factors().joinToString(","));
reading=2.500000,offset=1.000000

See also: argStr

argInt

argInt(int i) -> int | None

Read an argument as an integer.

An argument that was not written at the use site reads as None.

Parameters

i
The zero-based position of the argument.

Returns

The argument's integer value, or None when it was left out.

Examples

attribute Column { string col = ""; int width = 0; }
attribute Inspect { }

rule inspect {
    match @Inspect(i) on class C
    inject `Array<string> widths() => [ $for s in C.fields.where((f) => f.hasAttr("Column")).map((f) =>
        f.name + "=" + (f.attr("Column")?.argInt(1) ?? 255).toString()) : $s ];` at member of C
}

@Inspect
class User {
    @Column("full_name", 40) string name;
    @Column("id") int id;
}

console.writeln(User().widths().joinToString(","));
name=40,id=255

See also: argStr

argStr

argStr(int i) -> string | None

Read an argument as a string.

An argument that was not written at the use site reads as None, which lets a rule fall back to a default with ??.

Parameters

i
The zero-based position of the argument.

Returns

The argument's string value, or None when it was left out.

Examples

attribute Column { string col = ""; }
attribute Inspect { }

rule inspect {
    match @Inspect(i) on class C
    inject `Array<string> names() => [ $for s in C.fields.where((f) => f.hasAttr("Column")).map((f) =>
        f.attr("Column")?.argStr(0) ?? f.name) : $s ];` at member of C
}

@Inspect
class User {
    @Column("full_name") string name;
    @Column int id;
}

console.writeln(User().names().joinToString(","));
full_name,id

See also: argInt

See also

  • attr — Find an attribute on the field by name, with its argument values.
  • attr — Find an attribute on the method by name, with its argument values.