based on answers from other posts, here’s a snip of some code i use when reflecting over a top-level container (file scope):
inline for (@typeInfo(T).Struct.decls) |decl| {
const fld = @field(T, decl.name);
const FT = @TypeOf(fld);
const ti = @typeInfo(FT);
// do stuff
}
using ti
, i can determine whether fld
is a struct
; and using @hasDecl(FT, "feature")
, i can learn more about my duck-typed struct
type…
finally, i can execute fld.feature
to use this part of the struct
value…
my question/confusion occurs when my “struct” is actually the @import
of another zig file… in the earlier snip, fld
itself is already a type; i don’t seem to need the @TypeOf(fld)
to extract @typeInfo
… but what isn’t clear, is how do i actually interact with the (singleton??) value of this file-level container struct
???
conceptually, a top-level import like const Mod = @import("./Mod.zig")
seems to blur the line between a struct
type and its (singleton) value… selecting S.foo
is not the same as selecting s.foo
from some instance of S
…
and yet, when i write Mod.foo
i actually retrieve the value of the foo
decl – suggesting that Mod
in ths context is really a value…
(another dimension to my confusion is that we naturally use “decls” within a file-level container, compared with “fields” in a struct
type… i have discovered you can actually declare “fields” at file scope…)
so what am i really trying to do???
- i want to reflect on a file-level container, visiting all of its decls
- i want to find all such decls bound to some
@import("./Mod.zig")
- knowing these decls are
struct
’s, i then want to query whether they contain some other public decl of a well-known name; and finally - i want to retrieve the value of the decl found inside the imported file
to simplify the problem, these decls within the imported file are pub const
…
how do i do this???