You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was trying to use the type introspection features of the language on a struct, namely trying to access the field names composing the struct. When I went to print the values the program crashed. Subsequent runs fared similarly. I figured it might just be something with the name field of the Type_Info_Member struct so I tried looking up the offset and was receiving garbage values as well.
Not sure if the type introspection is still in a working state or not, but I figured I should let you know.
Below is my actual code in case I wasn't accessing the data appropriately.
#import "fmt.odin";
#import "mem.odin";
NewType :: struct {
field1: int,
field2: int,
field3: int,
}
main :: proc() {
newtype := NewType {
field1 = 10,
field2 = 11,
field3 = 12,
};
newtype_info := cast(^Type_Info.Struct) type_info_of_val( newtype );
newtype_members := newtype_info^.fields;
fmt.println( "", newtype_members[0].name ); // crash
fmt.println( "", newtype_members[0].offset ); // garbage value when i comment out the above line
}
The text was updated successfully, but these errors were encountered:
I came back to the preload file again and noticed there are other types like map and dynamic_array which currently don't appear to be part of the language yet. Perhaps I just jumped the gun and the struct type info hadn't been properly implemented up to this point v0.0.6. If that is the case I apologize for opening an issue Bill.
First, when casting a union to its subtype/tag, do:
newtype_info, ok := cast(^Type_Info.Struct) type_info_of_val(newtype);
This will return a boolean to check if it's valid. cast doesn't do any other checks (maybe I should make a raw_cast ).
Second, the kind of that type isn't a Type_Info.Struct but a Type_Info.Named. This means you want to get the base type of that Named type. This can be done manually or through the procedure type_info_base.
Third, check fmt.odin for a good exampe of what I mean.
Note: You don't need to do a manual dereference to get a struct's field as the language does an automatic one indirect dereference in those cases (as well as in the using cases). This is help with refactoring, and logic.
x: ^Foo;
// These two are identical in outputted code
x.^bar = 123;
x.bar = 123;
Hey Bill,
I was trying to use the type introspection features of the language on a struct, namely trying to access the field names composing the struct. When I went to print the values the program crashed. Subsequent runs fared similarly. I figured it might just be something with the name field of the Type_Info_Member struct so I tried looking up the offset and was receiving garbage values as well.
Not sure if the type introspection is still in a working state or not, but I figured I should let you know.
Below is my actual code in case I wasn't accessing the data appropriately.
The text was updated successfully, but these errors were encountered: