# Static comptime variable? (preserve state between two comptime function calls)

**URL:** <https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691>\
**Category:** Help\
**Tags:** comptime\
**Created:** [September 23, 2026, 1:12pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691 "2026-09-23T13:12:14Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![psznm](https://ziggit.dev/user_avatar/ziggit.dev/psznm/32/8377_2.png) [@psznm](https://ziggit.dev/u/psznm)\
**Post date:** [September 23, 2026, 1:12pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/1 "2026-09-23T13:12:14Z")

</div>

Is it possible to preserve some comptime state between comptime function calls?

What I am trying to do is to create a logger wrapper that provides IDs to log messages based on their name in the struct.

```zig
const UnnamedLogger = struct {
    logger: *Logger,
};
fn namedLog(This: type) LogFn(This) {
    comptime var LastT: type = This;
    const Static = struct {
        var i: u8 = 0;
    };
    if (This != LastT) {
        Static.i = 0;
        LastT = This;
    }
    const info = @typeInfo(This).@"struct";
    Static.i += 1;

    return (struct {
        const name = info.decls[Static.i].name;
        fn log(
            self: *const This,
            comptime level: LogLevel,
            comptime format: []const u8,
            args: anytype,
        ) void {
            const logger: *const UnnamedLogger = @ptrCast(@alignCast(self));
            logger.logger.log(level, This.name ++ "." ++ name ++ " " ++ format, args);
        }
    }).log;
}

```

And then usage would be

```zig
const Idk = struct {
    logger: *Logger,
    pub const name = "idk";
    pub const streamingBody = namedLog(@This());
    pub const streamingBody2 = namedLog(@This());
};

```

The issue here is that the stuff with `Static.i` does not work (and the `LastT` has the same issue), even though similiar with comptime var works, but the state can not be preserved between calls. And I can’t seem to be able to make any variation on `Static.i` work.

Is it impossible, or am I just missing something?

PS: I only really want to construct it this way because it is ZLS friendly. I think I should be able to construct my logger wrapper with `@Struct` but I think I would lose the ZLS autocompletion. So if you know of any zls friendly alternatives, please do let me know. The second best think I can think of is to change the API to

```zig
    pub const streamingBody = namedLog(@This(), 1);
    pub const streamingBody2 = namedLog(@This(), 2);

```

Which works, but maybe just a tiny bit annoying.

---

<div class="post-metadata">

**Author:** ![npc1054657282](https://ziggit.dev/user_avatar/ziggit.dev/npc1054657282/32/5790_2.png) [@npc1054657282](https://ziggit.dev/u/npc1054657282)\
**Post date:** [September 23, 2026, 2:05pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/2 "2026-09-23T14:05:57Z")

</div>

related

> [@Comptime-Mutable Memory Changes](https://ziggit.dev/t/comptime-mutable-memory-changes/3702):
>
> Hi everyone! Recently, PR [#19414](https://github.com/ziglang/zig/pull/19414) was merged, which more-or-less rewrote the compiler’s internal implementation of comptime var. This rewrite comes with some language changes, so I thought I’d explain the user-facing impact of this PR here so that people know what’s going on when they inevitably get errors TL;DR A pointer to a comptime var is never allowed to become runtime-known, to be contained within the resolved value of a declaration, or to be otherwise referenced by analysis of any declara…

---

<div class="post-metadata">

**Author:** ![psznm](https://ziggit.dev/user_avatar/ziggit.dev/psznm/32/8377_2.png) [@psznm](https://ziggit.dev/u/psznm)\
**Post date:** [September 23, 2026, 3:44pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/3 "2026-09-23T15:44:46Z")

</div>

Well, the post talks about capturing comptime var reference at runtime. I can see how something I was doing could be considered that, but check this.

```zig
fn Idk() type {
    comptime var counter: struct {
        i: u8 = 0,
        fn get(self: *@This()) u8 {
            self.i += 1;
            return self.i;
        }
    } = .{};
    return struct {
        logger: *Logger,
        pub const name = "idk";
        pub const streamingBody = namedLog(@This(), counter.get());
        pub const streamingBody2 = namedLog(@This(), counter.get());
    };
}

```

And compiler still says “captured value contains reference to comptime var”. No runtime involved. No comptime pointer being referenced from the created struct (or the factory function). I thought I could somehow manage with `@src`, but it can’t actually be used in struct declaration even if it is actually in a function. Non `comptime var` does not work either as it says the counter is not accessible, even though the context should always be comptime.

So I guess I am out of luck, and I will have to manually manage this. At least until there is LSP that undestands types created with `@Struct`. Which is unfortunate, because this feels like it should have been easy…

Is there really no way at all to find from within the `namedLog` what it is being assigned to? Any way at all (without manually providing the information).

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [September 23, 2026, 3:48pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/4 "2026-09-23T15:48:46Z")

</div>

declarations are evaluated in order of when they are seen used during analysis, so this wouldnt work even if you could have mutable comptime global state.

also this already exists as `std.log.scoped(.name)`

---

<div class="post-metadata">

**Author:** ![psznm](https://ziggit.dev/user_avatar/ziggit.dev/psznm/32/8377_2.png) [@psznm](https://ziggit.dev/u/psznm)\
**Post date:** [September 23, 2026, 3:52pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/5 "2026-09-23T15:52:02Z")

</div>

`std.log.scoped(.name)` is not nearly sufficient for my needs

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [September 23, 2026, 3:53pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/6 "2026-09-23T15:53:06Z")

</div>

yea i figured so after the fact

---

<div class="post-metadata">

**Author:** ![psznm](https://ziggit.dev/user_avatar/ziggit.dev/psznm/32/8377_2.png) [@psznm](https://ziggit.dev/u/psznm)\
**Post date:** [September 23, 2026, 3:54pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/7 "2026-09-23T15:54:54Z")

</div>

> [@vulpesx](#):
>
> declarations are evaluated in order of when they are seen used during analysis, so this wouldnt work even if you could have mutable comptime global state.

That is pretty good insight though, now it kinda makes sense why it is not possible to do it that way. Any clue why I can’t use `@src` in that function creating a structure? That would have probably allowed me to parse the name that is being assigned to.

---

<div class="post-metadata">

**Author:** ![npc1054657282](https://ziggit.dev/user_avatar/ziggit.dev/npc1054657282/32/5790_2.png) [@npc1054657282](https://ziggit.dev/u/npc1054657282)\
**Post date:** [September 23, 2026, 3:55pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/8 "2026-09-23T15:55:10Z")

</div>

> [@psznm](#):
>
> Well, the post talks about capturing comptime var reference at runtime.

There’s also one more rule that makes global comptime state impossible.

> [@Comptime-Mutable Memory Changes](https://ziggit.dev/t/comptime-mutable-memory-changes/3702/1):
>
> The second rule is that a pointer to a `comptime var` cannot be contained within the resolved value of a container-level `const` or `var` declaration. Intuitively, the `comptime var` is not allowed to “leak out” of the declaration that created it.

This does indeed lead to many comptime use cases not being met, but the upside is that all compilation units are independent of order.

> [@Comptime-Mutable Memory Changes](https://ziggit.dev/t/comptime-mutable-memory-changes/3702/1):
>
> Incremental compilation relies on the fact that global declarations are largely independent of one another, and their interdependencies can be easily modeled (for instance, one global declaration might depend on the resolved value of another). We also depend on the fact that the order of analysis of different declarations is (aside from some awkward edge cases, which eventually will either be ironed out or defined as implementation-defined behavior) unobservable. Unfortunately, global comptime state breaks both of these promises. Loads and stores of this state depend entirely on analysis order, and re-analyzing a declaration may mutate global state in ways which would not happen without incremental compilation. In short, the previous behavior was **completely incompatible with incremental compilation**.

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [September 23, 2026, 3:58pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/9 "2026-09-23T15:58:55Z")

</div>

`@src` can only be used in function bodies

---

<div class="post-metadata">

**Author:** ![psznm](https://ziggit.dev/user_avatar/ziggit.dev/psznm/32/8377_2.png) [@psznm](https://ziggit.dev/u/psznm)\
**Post date:** [September 23, 2026, 4:00pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/10 "2026-09-23T16:00:18Z")

</div>

> [@npc1054657282](#):
>
> There’s also one more rule that makes global comptime state impossible.

I feel like the last example can be hardly considered global comptime state… It certainly feels pretty well contained. When I thought of that I thought it would work for sure… it still feels counterintuitive that the same thing that would likely be trivial with `@Struct` can’t really be expressed in this more verbose way. Oh well…

---

<div class="post-metadata">

**Author:** ![psznm](https://ziggit.dev/user_avatar/ziggit.dev/psznm/32/8377_2.png) [@psznm](https://ziggit.dev/u/psznm)\
**Post date:** [September 23, 2026, 4:01pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/11 "2026-09-23T16:01:25Z")

</div>

I mean, it IS in a function body… Sure, there is a container in between, but still…

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [September 23, 2026, 4:07pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/12 "2026-09-23T16:07:26Z")

</div>

even if that was allowed it would give you the function name, not the name of the decl of the struct within the function

---

<div class="post-metadata">

**Author:** ![psznm](https://ziggit.dev/user_avatar/ziggit.dev/psznm/32/8377_2.png) [@psznm](https://ziggit.dev/u/psznm)\
**Post date:** [September 23, 2026, 4:10pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/13 "2026-09-23T16:10:51Z")

</div>

Fair, didn’t realize that. Never actually used it and always thought it points to where the @src was called. So there is no way at all to be able to determine what name the value is being assigned to, without actually providing some information manually.

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [September 23, 2026, 4:11pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/14 "2026-09-23T16:11:30Z")

</div>

> [@psznm](#):
>
> So there is no way at all to be able to determine what name the value is being assigned to, without actually providing some information manually.

that is correct

---

<div class="post-metadata">

**Author:** ![psznm](https://ziggit.dev/user_avatar/ziggit.dev/psznm/32/8377_2.png) [@psznm](https://ziggit.dev/u/psznm)\
**Post date:** [September 23, 2026, 4:24pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/15 "2026-09-23T16:24:57Z")

</div>

Actually the @src _does_ point to the place where it was called. Multiple @src in the same function do give different results. So if the @src was usable there, it would allow me to determine what it is being assigned to.

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [September 23, 2026, 4:31pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/16 "2026-09-23T16:31:15Z")

</div>

no thats because monomorphisation changes the function name, i.e its the comptime parameters not the place its being assigned to.

---

<div class="post-metadata">

**Author:** ![psznm](https://ziggit.dev/user_avatar/ziggit.dev/psznm/32/8377_2.png) [@psznm](https://ziggit.dev/u/psznm)\
**Post date:** [September 23, 2026, 4:35pm UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/17 "2026-09-23T16:35:31Z")

</div>

Well, I thought the `file` field it returns is actually the file content so that I would be able to parse the decl name. Now that I am looking at it its just file name. Still, I would have probably been able to do something like

```zig
    const start = @src();
    return struct {
        logger: *Logger,
        pub const name = "idk";
        pub const streamingBody = namedLog(@This(), start, @src());
        pub const streamingBody2 = namedLog(@This(), start, @src());
    };

```

And infer the decl index from the line numbers. But damn, would that be hacky and fragile.

---

<div class="post-metadata">

**Author:** ![psznm](https://ziggit.dev/user_avatar/ziggit.dev/psznm/32/8377_2.png) [@psznm](https://ziggit.dev/u/psznm)\
**Post date:** [September 24, 2026, 8:32am UTC](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691/18 "2026-09-24T08:32:25Z")

</div>

So I ended up duplicating the decl name into the function factory and adding a comptime test for that. The comptime test was very tricky to get right, took me maybe 2 hours, because there is some weirdness in how strings are compared in comptime and so the newly created function in the test was different from the function created outside of the test. The solution was to do this in the factory function: instead of using provided name directly, find the decl with that name from `@This()`, and use the `decl.name`. If I had gone the index based way I wouldn’t have to deal with that gotcha but there are issues with index based that I was unhappy with.

```zig
fn testLogger(This: type) void {
    comptime {
        const info = @typeInfo(This).@"struct";

        for (info.decls[1..]) |decl| {
            if (@field(This, decl.name) != namedLogStr(This, decl.name)) {
                @compileError(This.name ++ "." ++ decl.name ++ " has incorrectly specified id. Must be ." ++ decl.name);
            }
        }
    }
}
const Idk = struct {
    logger: *Logger,
    pub const name = "idk";
    pub const streamingBody = namedLog(@This(), .streamingBody);
    pub const streamingBody2 = namedLog(@This(), .streamingBody); // error: idk.streamingBody2 has incorrectly specified id. Must be .streamingBody2
    comptime {
        testLogger(@This());
    }
};

```
