-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Proposal: Make comptime var convert to runtime var when modified at runtime #9898
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
How does this interact with pointers and addressof ( |
Should be same as storing into runtime variable and using that. Maybe we can add some special cases for pointers (e.g convert to const ptr when out of scope), maybe not |
I'm not sure I see the benefit of this. Throwing an error when a comptime var is modified at runtime looks like reasonable behavior to me. Why would we want to silently throw out the comptime annotation instead? Actually, what would be the point of making a variable |
The comptime annotation is not thrown out, the variable is comptime until it is runtime modified, that is it can do comptime work until the first run-time modification to it, so the point of it being comptime remains until you try to modify it at runtime. The point of placing the last comptime value at the instantiation site is for scoping runtime access to it the same way as for runtime variables, rather than the current situation of closing over it (#7396) in a broken way. |
So what would this program do? const std = @import("std");
pub fn main() void {
var x: u32 = 0;
const px = &x; // x is not yet modified, what does this point to?
if (runtime_cond) x += 1; // conditionally modify x
const px2 = &x; // x is definitely runtime now
std.debug.print("ptrs_eql={}, vals_eql={}\n", .{ px == px2, px.* == px2.* });
} |
@Matceporial Edit: But my other point still stands: With this change, what is the point of a |
Personally, when I explicitly use |
Uh oh!
There was an error while loading. Please reload this page.
This also solves #1470, #906 and #7396, while being better at preserving variable semantics
becomes equivalent to
Essentially, a comptime variable is converted into a runtime var with its last comptime value at the instantiation site if it is modified at runtime.
#7396 becomes the same as closing over a runtime var.
The text was updated successfully, but these errors were encountered: