# Comptime reflection on namespaces?

**URL:** <https://ziggit.dev/t/comptime-reflection-on-namespaces/9907>\
**Category:** Help\
**Tags:** language\
**Created:** [April 30, 2025, 8:49am UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907 "2025-04-30T08:49:56Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsdtas](https://ziggit.dev/user_avatar/ziggit.dev/tsdtas/32/3512_2.png) [@tsdtas](https://ziggit.dev/u/tsdtas)\
**Post date:** [April 30, 2025, 8:49am UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/1 "2025-04-30T08:49:56Z")

</div>

Consider this:

```zig
const foobar = struct{
    const foo = @import("foo"); 
    const bar = @import("bar");
};

@compileLog(@typeInfo(foobar));
@compileLog(comptime std.meta.declarations(foobar));

```

→

```zig
error: found compile log statement

Compile Log Output:
@as(builtin.Type, .{ .@"struct" = .{ .layout = .auto, .backing_integer = null, .fields = &.{}[0..0], .decls = &.{}[0..0], .is_tuple = false } })
@as([]const builtin.Type.Declaration, &.{}[0..0])

```

Why don’t imported modules show up anywhere in the type information?

---

<div class="post-metadata">

**Author:** ![cryptocode](https://ziggit.dev/user_avatar/ziggit.dev/cryptocode/32/817_2.png) [@cryptocode](https://ziggit.dev/u/cryptocode)\
**Post date:** [April 30, 2025, 8:52am UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/2 "2025-04-30T08:52:09Z")

</div>

Make declarations `pub` and they’ll show up

---

<div class="post-metadata">

**Author:** ![castholm](https://ziggit.dev/user_avatar/ziggit.dev/castholm/32/1330_2.png) [@castholm](https://ziggit.dev/u/castholm)\
**Post date:** [April 30, 2025, 8:52am UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/3 "2025-04-30T08:52:56Z")

</div>

`@typeInfo` only returns `pub` declarations. This is the case even if called from the same file as the struct declaration, where non-`pub` would otherwise be accessible.

---

<div class="post-metadata">

**Author:** ![tsdtas](https://ziggit.dev/user_avatar/ziggit.dev/tsdtas/32/3512_2.png) [@tsdtas](https://ziggit.dev/u/tsdtas)\
**Post date:** [April 30, 2025, 8:57am UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/4 "2025-04-30T08:57:35Z")

</div>

… it seems obvious in hindsight. Thank you. 🙂

For reference:

```zig
const foobar = struct{
    pub const foo = @import("foo"); 
    pub const bar = @import("bar");
};

```

works.

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [April 30, 2025, 10:32am UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/5 "2025-04-30T10:32:06Z")

</div>

BTW: I never thought of this construction!  
It can help to avoid local var / parameter nameclashes.

```zig
const using = struct
{
    const board = @import("board.zig");
};

const Board = using.board.Board;

// now I can use "board" as variable / parameter name

```

---

<div class="post-metadata">

**Author:** ![tsdtas](https://ziggit.dev/user_avatar/ziggit.dev/tsdtas/32/3512_2.png) [@tsdtas](https://ziggit.dev/u/tsdtas)\
**Post date:** [April 30, 2025, 12:07pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/6 "2025-04-30T12:07:58Z")

</div>

For what it’s worth, you can mostly avoid running into this problem if you follow the convention of making imported types into filestructs instead:

Instead of

```zig
// in 'board.zig' top level
pub const Board = struct {
    width: f32, 
    height: f32, 
    has_hole: bool,
};

```

do this

```zig
// in 'Board.zig' top level
pub const Board = @This();
width: f32,
height: f32,
has_hole: bool,

```

then when you import it, import it directly as the type

```zig
const Board = @import("Board.zig"); // this equivalent to the old "board.Board"

// "board" now free to be a variable name
const board = Board.init(1.23, 4.56, false);

```

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [April 30, 2025, 12:12pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/7 "2025-04-30T12:12:14Z")

</div>

Thanks. Yes very true. Coming from other languages I am not used yet to “a file is a struct”.  
edit: on the other side I like types to have uppercase names and files lowercase 🙂

---

<div class="post-metadata">

**Author:** ![tsdtas](https://ziggit.dev/user_avatar/ziggit.dev/tsdtas/32/3512_2.png) [@tsdtas](https://ziggit.dev/u/tsdtas)\
**Post date:** [April 30, 2025, 12:16pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/8 "2025-04-30T12:16:21Z")

</div>

> on the other side I like types to have uppercase names and files lowercase

I agree. I do this:

`const Board = @import("board.zig");`

The Zig compiler doesn’t though, so I wouldn’t consider it canonical.

---

<div class="post-metadata">

**Author:** ![cryptocode](https://ziggit.dev/user_avatar/ziggit.dev/cryptocode/32/817_2.png) [@cryptocode](https://ziggit.dev/u/cryptocode)\
**Post date:** [April 30, 2025, 12:21pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/9 "2025-04-30T12:21:54Z")

</div>

I think sticking to PascalCase on filenames that represents structs is a very useful convention.

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [April 30, 2025, 12:22pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/10 "2025-04-30T12:22:17Z")

</div>

That is a possibility yes.  
In any case: I like the nameclash shadow problem solution!

---

<div class="post-metadata">

**Author:** ![tsdtas](https://ziggit.dev/user_avatar/ziggit.dev/tsdtas/32/3512_2.png) [@tsdtas](https://ziggit.dev/u/tsdtas)\
**Post date:** [April 30, 2025, 12:30pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/11 "2025-04-30T12:30:47Z")

</div>

I do agree, but I would rather not depend on file properties that aren’t universally semantic or supported. Same reason I don’t use spaces in filenames even though that is perfectly valid too.

---

<div class="post-metadata">

**Author:** ![cryptocode](https://ziggit.dev/user_avatar/ziggit.dev/cryptocode/32/817_2.png) [@cryptocode](https://ziggit.dev/u/cryptocode)\
**Post date:** [April 30, 2025, 12:32pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/12 "2025-04-30T12:32:53Z")

</div>

It’s just casing, and std already does it anyway

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [April 30, 2025, 12:54pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/13 "2025-04-30T12:54:55Z")

</div>

Maybe offtopic but anyway:  
What happens if I define a const array in consts.zig

```zig
VALUES: [256]u8 = ....

```

and refer to it in someotherfile.zig  
with

```zig
const consts = @import("consts.zig");
const VALUES = consts.VALUES;

```

Suddenly I was wondering if I do not have a local copy!!

---

<div class="post-metadata">

**Author:** ![chung-leong](https://ziggit.dev/user_avatar/ziggit.dev/chung-leong/32/999_2.png) [@chung-leong](https://ziggit.dev/u/chung-leong)\
**Post date:** [April 30, 2025, 1:57pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/14 "2025-04-30T13:57:07Z")

</div>

It’s not that obvious. Prior to version 0.11.0 all declarations were listed and there’s an `is_pub` field in [Declaration](https://ziglang.org/documentation/0.10.0/std/#root;builtin.Type.Declaration).

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [April 30, 2025, 2:43pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/15 "2025-04-30T14:43:17Z")

</div>

It is a copy (using @intFromPtr)

```zig
140702043666519 // address of original
140702043666744 // address of referenced

```

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [April 30, 2025, 2:46pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/16 "2025-04-30T14:46:37Z")

</div>

HOWEVER: in release mode the addresses are the same 🙂

```zig
140696585111110
140696585111110

```

---

<div class="post-metadata">

**Author:** ![cryptocode](https://ziggit.dev/user_avatar/ziggit.dev/cryptocode/32/817_2.png) [@cryptocode](https://ziggit.dev/u/cryptocode)\
**Post date:** [April 30, 2025, 2:49pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/17 "2025-04-30T14:49:19Z")

</div>

Due to constant folding by the optimizer

---

<div class="post-metadata">

**Author:** ![slonik-az](https://ziggit.dev/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@slonik-az](https://ziggit.dev/u/slonik-az)\
**Post date:** [April 30, 2025, 3:05pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/18 "2025-04-30T15:05:15Z")

</div>

> [@tsdtas](#):
>
> … it seems obvious in hindsight. Thank you

In hindsight it is, indeed, obvious. But I think that every newcomer to Zig trips over this one. Should be part of FAQ.

---

<div class="post-metadata">

**Author:** ![cztomsik](https://ziggit.dev/user_avatar/ziggit.dev/cztomsik/32/3032_2.png) [@cztomsik](https://ziggit.dev/u/cztomsik)\
**Post date:** [April 30, 2025, 8:23pm UTC](https://ziggit.dev/t/comptime-reflection-on-namespaces/9907/19 "2025-04-30T20:23:05Z")

</div>

File-structs cannot be used with tagged unions or generic functions. If you think about it, it’s rather anti-pattern because it limits free evolution of the codebase (and/or easy refactors).
