Uh oh!
There was an error while loading. Please reload this page.
std.meta: add comptimeKnown function - #21557
Conversation
alexrp
commented
Jan 26, 2025
Hmm, that said, I seem to vaguely recall @mlugg wanting to change the semantics around implicit |
mlugg
commented
Jan 26, 2025
What I really want to do is remove However... it's a blatant hack. I think if there's sufficient justification to warrant this functionality (somewhat debatable at this point in time IMO), we should just have a builtin for it. |
alexrp
commented
Aug 24, 2025
Closing per @mlugg's comment above. If we want this, it should be a builtin. |
This function returns whether a value is comptime known.
I felt this function necessary to add to std.meta because it could be used to solve a few issues in the standard library and is overall just very useful for users as well. Off the top of my head, I can think of two immediate use cases in the standard library. Firstly,
std.zig.c_builtins.__builtin_constant_pcould be updated to work like it does in C rather than always returning 0, and more importantly, in #21498 there is some discussion aroundstd.StaticStringMapregarding whether we should trade off the ability to have runtime keys for some strong optimizations which can be performed with comptime-known maps. With this function, we can have it both ways, and automatically choose whether to use the current implementation which can work for runtime maps, or the proposed optimized version which only works on comptime-known maps. Some follow up work may be considered to implement these changes, but for now I am just keeping it simple and just posting the function itself.