Skip to content

std.meta: add comptimeKnown function #21557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

SeanTheGleaming
Copy link
Contributor

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 around std.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.

Copy link
Member

@alexrp alexrp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The std.zig.c_builtins.__builtin_constant_p use case seems compelling to me at least. I suspect @andrewrk will have thoughts on this though, given the debate around @inComptime.

@alexrp alexrp requested a review from andrewrk January 26, 2025 18:49
@alexrp
Copy link
Member

alexrp commented Jan 26, 2025

Hmm, that said, I seem to vaguely recall @mlugg wanting to change the semantics around implicit comptimeness of tuple fields. Is that right? If so, we may not want to commit to this API unless there's an alternative implementation strategy.

@mlugg
Copy link
Member

mlugg commented Jan 26, 2025

Hmm, that said, I seem to vaguely recall @mlugg wanting to change the semantics around implicit comptimeness of tuple fields. Is that right? If so, we may not want to commit to this API unless there's an alternative implementation strategy.

What I really want to do is remove comptime fields, but I'm not sure whether that's viable. I think this will continue working as long as comptime fields exist.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants