Modules

Reflection

moduleSource

Available on macOS, Linux, Windows, Wasm

#import "Reflection";

Helpers for working with type information at run time: integer ranges, struct members, enum names and values, and setting a value from text.

Example↗
Color :: enum { RED; GREEN; BLUE; }
assert(enum_value_to_name(Color.GREEN) == "GREEN");

value: Color;
assert(set_value_from_string(value, "BLUE"));
assert(value == .BLUE);

Procedures#

(pointer: *void, info: *Type_Info_Array) -> (count: s64, data: *void)

The element count and data pointer of the array at pointer, whose type is info. Works for fixed, resizable and view arrays alike.

values := int.[4, 5, 6];
count, data := get_array_count_and_data(*values, cast(*Type_Info_Array) type_info(type_of(values)));
assert(count == 3 && (cast(*int) data).* == 4);
↗
(value: $T, info: *Type_Info_Integer, pointer: *void) -> (success: bool, low: T, high: T)

Stores value at pointer as the integer type info describes, if it fits. Returns whether it did, and the lowest and highest value of that type.

small: u8;
ok := range_check_and_store(200, cast(*Type_Info_Integer) type_info(u8), *small);
assert(ok && small == 200);
ok = range_check_and_store(300, cast(*Type_Info_Integer) type_info(u8), *small);
assert(!ok && small == 200);
↗
(info: *Type_Info_Struct, name: string) -> *Type_Info_Struct_Member

The member called name of a struct, or null. Unlike get_field in Basic it does not look inside using members.

Point :: struct { x: int; y: int; }
info := cast(*Type_Info_Struct) type_info(Point);
assert(get_struct_field_info(info, "y") != null);
assert(get_struct_field_info(info, "z") == null);
↗
($T: Type, name: string) -> (T, bool)

The member of the enum T called name, and whether there is one.

Color :: enum { RED; GREEN; BLUE; }
value, found := enum_name_to_value(Color, "GREEN");
assert(found && value == .GREEN);
_, found = enum_name_to_value(Color, "PINK");
assert(!found);
↗
(e_value: $Enum) -> string

The name of an enum value, or an empty string when it matches no member.

Color :: enum { RED; GREEN; BLUE; }
assert(enum_value_to_name(Color.BLUE) == "BLUE");
↗
(data: *void, info: *Type_Info_Enum) -> s64

Reads the enum stored at data, whose type is info, as an s64.

Level :: enum u8 { LOW :: 3; HIGH :: 9; }
level := Level.HIGH;
assert(get_enum_value(*level, cast(*Type_Info_Enum) type_info(Level)) == 9);
↗
(data: *void, info: *Type_Info_Enum, value: s64)

Stores value in the enum at data, whose type is info. The value is not checked against the members.

Level :: enum u8 { LOW :: 3; HIGH :: 9; }
level: Level;
set_enum_value(*level, cast(*Type_Info_Enum) type_info(Level), 9);
assert(level == .HIGH);
↗
(any: Any, text: string) -> bool
(any: Any, text: *string) -> bool

Reads text into the value any points to: a bool (true or false), an integer, a float, a quoted string or an enum member name. Returns whether it worked; the value is left alone when it did not. The number must fit the type.

count: u8;
assert(set_value_from_string(count, "200"));
assert(count == 200);
assert(!set_value_from_string(count, "300"));
↗