diff options
| author | gingerBill <bill@gingerbill.org> | 2019-08-11 23:58:49 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2019-08-11 23:58:49 +0100 |
| commit | 04036aba9c1eec05fe143d939ef93e017502f015 (patch) | |
| tree | 0d9da264ef729dc01822a6be975cfb8f03fc531f /examples | |
| parent | b08aa857b3c99c241e93ea3d0fbe36f712d96015 (diff) | |
`package reflect`; fix substring type bug; fix scoping rules for `using` on procedure parameter
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/demo/demo.odin | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/examples/demo/demo.odin b/examples/demo/demo.odin index e07eea7c7..157ecb9bf 100644 --- a/examples/demo/demo.odin +++ b/examples/demo/demo.odin @@ -4,6 +4,7 @@ import "core:fmt" import "core:mem" import "core:os" import "core:runtime" +import "core:reflect" when os.OS == "windows" { import "core:thread" @@ -946,26 +947,39 @@ deferred_procedure_associations :: proc() { } } -struct_field_tags :: proc() { - fmt.println("# struct_field_tags"); +reflection :: proc() { + fmt.println("# reflection"); Foo :: struct { x: int `tag1`, - y: string `tag2`, + y: string `json:"y_field"`, z: bool, // no tag } - ti := runtime.type_info_base(type_info_of(Foo)); - s := ti.variant.(runtime.Type_Info_Struct); + id := typeid_of(Foo); + names := reflect.struct_field_names(id); + types := reflect.struct_field_types(id); + tags := reflect.struct_field_tags(id); + + assert(len(names) == len(types) && len(names) == len(tags)); + fmt.println("Foo :: struct {"); - for _, i in s.names { - if tag := s.tags[i]; tag != "" { - fmt.printf("\t%s: %T `%s`,\n", s.names[i], s.types[i], tag); + for tag, i in tags { + name, type := names[i], types[i]; + if tag != "" { + fmt.printf("\t%s: %T `%s`,\n", name, type, tag); } else { - fmt.printf("\t%s: %T,\n", s.names[i], s.types[i]); + fmt.printf("\t%s: %T,\n", name, type); } } fmt.println("}"); + + + for tag, i in tags { + if val, ok := reflect.struct_tag_lookup(tag, "json"); ok { + fmt.printf("json: %s -> %s\n", names[i], val); + } + } } main :: proc() { @@ -986,6 +1000,6 @@ main :: proc() { bit_set_type(); diverging_procedures(); deferred_procedure_associations(); - struct_field_tags(); + reflection(); } } |