std.meta: add comptimeKnown function #21557
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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_p
could be updated to work like it does in C rather than always returning 0, and more importantly, in #21498 there is some discussion aroundstd.StaticStringMap
regarding 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.