# Checking if a value is comptime-known

**URL:** <https://ziggit.dev/t/checking-if-a-value-is-comptime-known/18493>\
**Category:** Help\
**Tags:** language, comptime\
**Created:** [November 17, 2026, 12:22pm UTC](https://ziggit.dev/t/checking-if-a-value-is-comptime-known/18493 "2026-11-17T12:22:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [November 17, 2026, 12:22pm UTC](https://ziggit.dev/t/checking-if-a-value-is-comptime-known/18493/1 "2026-11-17T12:22:35Z")

</div>

Is there a nicer way of writing this?

```zig
inline fn isComptimeKnown(value: usize) bool {
    return @TypeOfT@aspeOf(@as([*]u8, @ptrFromInt(1))[0..value]) != []u8;
}

```

I use it to encode a comptime-known value as an immediate in inline assembly (an optimization LLVM intentionally cannot do):

```zig
pub inline fn write(comptime name: []const u8, value: usize) void {
    if (isComptimeKnown(value) and value >> 12 == 0) {
        asm volatile (std.fmt.comptimePrint("csrwi {s}, {d}", .{ name, value }));
        return;
    }

    asm volatile (std.fmt.comptimePrint("csrw {s}, %[value]", .{name})
        :
        : [value] "r" (value),
    );
}

```

---

<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:** [November 17, 2026, 12:35pm UTC](https://ziggit.dev/t/checking-if-a-value-is-comptime-known/18493/2 "2026-11-17T12:35:08Z")

</div>

do you think this is nicer?

```zig
inline fn isComptimeKnown(value: anytype) bool {
    const tuple = .{ value };
    return @typeInfo(@TypeOf(tuple)).@"struct".field_attrs[0].@"comptime";
}

```

However, `comptime` fields are [going to be removed](https://codeberg.org/ziglang/zig/issues/32045) (they are super buggy anyway)

to work around that you can do this, which might be nicer anyway

```zig
inline fn isComptimeKnown(value: anytype) bool {
    // explicit type needed while `comptime` fields still exist
    const tuple: struct { @TypeOf(value), u8 } = .{ value, 1 };
    return @TypeOf(""[0..tuple[1]]) != []const u8;
}

```

Both of these work for any type, unlike yours which only works for integers  
which could be reduced by using `""` instead of `@ptrFromInt`

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [November 17, 2026, 12:59pm UTC](https://ziggit.dev/t/checking-if-a-value-is-comptime-known/18493/3 "2026-11-17T12:59:22Z")

</div>

Thanks for the extension to any type!

> which could be reduced by using `""` instead of `@ptrFromInt`

I guess `"".ptr` would work, cool 🙂

It’s kind of unfortunate that these means are so cursed 😕 it’d be nice if this was officially supported

---

<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:** [November 17, 2026, 1:00pm UTC](https://ziggit.dev/t/checking-if-a-value-is-comptime-known/18493/4 "2026-11-17T13:00:05Z")

</div>

`.ptr` is not required

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [November 17, 2026, 1:01pm UTC](https://ziggit.dev/t/checking-if-a-value-is-comptime-known/18493/5 "2026-11-17T13:01:17Z")

</div>

My bad, I forgot that it would be fine due to `@TypeOf`

---

<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:** [November 17, 2026, 1:03pm UTC](https://ziggit.dev/t/checking-if-a-value-is-comptime-known/18493/6 "2026-11-17T13:03:15Z")

</div>

it isn’t a meaningful difference here, but `.ptr` does remove the length safety check. Speaking of my snippets should use `0` instead of `1` just to be safe.

`@TypeOf` [may be removed](https://codeberg.org/ziglang/zig/issues/32099) and the replacement won’t guarantee only type analysis, `@TypeOf` also currently doesn’t guarantee it in some edge cases though it is supposed to.
