zigo
June 23, 2026, 8:57am
1
Why isn’t it bi-directional?
const print = @import("std").debug.print;
pub fn main() void {
var v: usize = 123;
const x = &v;
print("{}\n", .{ @TypeOf(x) });// *usize
const y = @constCast(x);
print("{}\n", .{ @TypeOf(y) });// *usize
const z: *const usize = y;
print("{}\n", .{ @TypeOf(z) });// *const usize
const w = @constCast(z);
print("{}\n", .{ @TypeOf(w) });// *usize
}
Because pointers-to-var coerce to pointers-to-const without any checks or manipulation. It’s always safe and always a no-op. There’s no need for a builtin.
4 Likes
what do you mean bi-directional? if you mean var -> const then the builtin should be named @constToggle()
because
var v: usize = 123;
const x = &v; // *usize
// it coerces
const y: *const usize = x; // just works
6 Likes
I was writing your exact snippet, but then realized he already used it for z
3 Likes
zigo
June 23, 2026, 9:49am
6
When I first time saw the @constCast function, I thought it will add the const modifier.
Maybe @constToggle can be used in some generic functions. (I have not found any practical use cases though.)
1 Like