Helpers for working with type information at run time: integer ranges, struct members, enum names and values, and setting a value from text.
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);
(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 );Reset Copy ↗ Run ▸
(size_in_bytes: int ) -> (low: s64 , high: s64 )
The lowest and highest signed value that fits in size_in_bytes bytes (1, 2, 4 or 8).
(size_in_bytes: int ) -> (low: u64 , high: u64 )
The lowest and highest unsigned value that fits in size_in_bytes bytes (1, 2, 4 or 8).
(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 );Reset Copy ↗ Run ▸
(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.
($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);Reset Copy ↗ Run ▸
(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" );Reset Copy ↗ Run ▸
(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 );Reset Copy ↗ Run ▸
(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);Reset Copy ↗ Run ▸
(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.