Why @constCast need explict type in 0.14?

test "const cast" {
    // myfn(@constCast(&.{.count = 0})); //does not work in zig 0.14 anymore
    myfn(@constCast(&SuperLongName______________________________Struct{.count = 0}));
    constArgFn(&.{.count = 0});

}

const SuperLongName______________________________Struct = struct {
    count: usize,
};


fn myfn(arg: *SuperLongName______________________________Struct) void {
    std.debug.print("count={}\n", .{arg.count});
}


fn constArgFn(arg: *const SuperLongName______________________________Struct) void {
    std.debug.print("count={}\n", .{arg.count});
}

I believe this is the relevant part of the release notes:

Remove Anonymous Struct Types, Unify Tuples

3 Likes

By the way, that’s illegal behavior. In your code the SuperLongName is a const temporary. If you want it to be mutable, put it in a var.

Im pretty sure its ok to @constCast temporaries. It would be illigal behavior only if you try to actually mutate value. Here is example of doing it in compiler source code:

Sure but I assumed in the non-toy code OP would be mutating the value. Otherwise they could just change the pointer type to const and wouldn’t need the cast in the first place.

thx for the advice.
since @constCast was provided, there is a need for it, i think this is a place.

thx.
not notice the behavior before.
now i find that, mutation is ill only in safe or debug mode.