LEVIATHAN vd52e79c4 · d52e79c486

Standard Library · meta

class Field

A field of a class, as a rule sees it.

since 0.1.0-alpha.1linuxwindowswasm

Examples

use io::console;

attribute Inspect { }

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

@Inspect
class Holder {
    string name;
    int? maybe;
    Array<int> items;
    Map<string, int> counts;
}

console.writeln(Holder().types().joinToString("; "));
name:string; maybe:int | None; items:Array<int>; counts:Map<string, int>

Fields

attributes

Array<meta::Attr> attributes

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

attrs

Array<string> attrs

The names of the attributes written on the field.

isPrivate

bool isPrivate

true for a field declared private. Reflection reads private fields too.

isProtected

bool isProtected

true for a field declared protected.

isPublic

bool isPublic

true for a field declared public or with no access marker.

isStatic

bool isStatic

true for a static field, which meta::Class.staticFields lists.

name

string name

The field's name.

type

string type

The field's type, spelled as the source wrote it. An optional int? reads int | None.

Methods

attr

attr(string n) -> meta::Attr | None

Find an attribute on the field by name, with its argument values.

Parameters

n
The attribute name, without the @.

Returns

The attribute, or None when the field does not carry it.

Examples

use io::console;

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

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

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

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

See also: Attr

hasAttr

hasAttr(string n) -> bool

Check whether the field carries an attribute of a given name.

Parameters

n
The attribute name, without the @.

Returns

true when the field has that attribute.

Examples

use io::console;

attribute Hidden { }
attribute Inspect { }

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

@Inspect
class Account {
    string user;
    @Hidden string password;
    int id;
}

console.writeln(Account().visible().joinToString(","));
user,id

See also

  • Class — A class that a rule has matched, as a rule sees it.