This prevents bugs from occurring from changing the type in one place (the return type in this example), but not the other. If you need the previous semantics, you can get them with @as:
For anyone who hasnât checked out the material in the wiki who is wondering how this works, (and for my own understanding) â tldrâŚ
From the wiki:
All of these builtins used to take two parameters; now, they only take one (the value to cast). The type to cast to is inferred based on the expressionâs result type .
// old version, y will be deduced as u8
const y = @intCast(u8, x);
// new version, y will be declared and the cast will be deduced
const y: u8 = @intCast(x);
This is only one application - there are a number of built-ins that are effected by this change. There is also some important stuff about pointer casting rules. Itâs enough of a difference that I suggest you check it out - itâs worth reading.
At first glance I thought âthis doesnât line-up with the âalways be explicitâ nature of Zig.â I mean, the old way showed you explicitly right there at the use site what the result type would be. But after reading the full explanation and seeing how it can eliminate bugs related to updating result types in various places, I totally get it. And as the wiki article says, now these built-ins serve more like markers in the code to signal âhey, a cast is allowed and gonna happen here.â
Yeah, I think the main intent is to just remove a degree of freedom that is uncessary and can even cause issues with activities like aggressive refactoring. It took me a minute to think it through, but if itâs well implemented, then I really like it actually.
Love this change, really excited for it! Casting of numeric types was always one of my biggest issues with Zig. It was so much typing and made the code in my opinion really hard to read.
Does anyone know when this will fully be implemented? I wonder if it makes more sense to update my Zig now or in a few days.
It has already landed in master branch, will be part of the upcoming 0.11.0 release, and is already available in the master branch builds on the download page.