-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
proposal: isComptime builtin. #7539
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
Comments
originally I had the idea for a function that would take a value of type T and return a comptime ?T (which would be null if the value isn't comptime), but it turns out this isn't necessary, as this works:
|
Duplicate of #868. |
@Vexu that issue is about whether the function is being a called from a |
A value will only be |
This just isn't true at all...
|
|
@Vexu Perhaps, but how do you explain this?
|
If you declare a |
@Vexu, ok, but that doesn't change the fact that your original statement was wrong. My point is that in this case |
|
@Vexu |
I got that wrong but |
Thanks for the proposal. It is planned to not have this builtin because the language intends for Zig programmers to write code that works both at comptime and runtime values. There is no need to explicitly check, and having such a builtin would lead to its use, making code needlessly more complex. |
The language also has operators that only work on comptime values, as well as lots of stuff that only works at runtime. Add to this the fact that it is already possible to tell if values are comptime by abusing |
Possible use case: A more user-friendly implementation of pub fn assert(condition: bool) void {
if (!condition) {
if (@isComptime()) {
// because @panic(...) doesn't show the message at compile time
@compileError("assertion failed");
}
switch (comptime std.builtin.mode) {
.Debug, .ReleaseSafe => @panic("assertion failed"),
.ReleaseFast, .ReleaseSmall => unreachable,
}
}
}
However, this could also be implemented using the |
This comment was marked as off-topic.
This comment was marked as off-topic.
@gooncreeper Just for the record, it's currently possible to discover whether or not a value is comptime-known by instantiating an anonymous tuple and reflecting on its fields, which in turn makes it possible to implement an const std = @import("std");
const expectEqual = std.testing.expectEqual;
inline fn isComptimeKnown(val: anytype) bool {
return @typeInfo(@TypeOf(.{val})).Struct.fields[0].is_comptime;
}
test {
const a: i32 = 15;
try expectEqual(true, isComptimeKnown(a));
var x: u64 = undefined;
x = 15;
try expectEqual(false, isComptimeKnown(x));
try expectEqual(true, isComptimeKnown(x >= 0));
} (Adapted from #868 (comment)) |
currently (as far as I know), there is no simple to get whether a value is known at compile time. For integer types, you can do
@TypeOf(@clz(u8, val)) == comptime_int
, but this is really more of a hack. Since this check already exists as part of@clz
, it shouldn't be hard to implement into zig.The text was updated successfully, but these errors were encountered: