-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
remove the concept of explicit casting as it currently exists #1061
Comments
I don't agree with removing it completely, since there are multiple cases where it would just add verbosity and some cases you need an explicit cast (unicode is a use case for this, and the std lib has quite a few other cases where it is used), especially with the introduction of 'arbitrary' length integers the use of explicit casts I feel designates extent clear enough. However I'm all for a 'shorten' cast. I don't see a use case for floats or other types though, so I'm all for removing those kind of explicit casts and just having a builtin for shortening ints. Edit: just to clarify, I'm all for removing explicit casts and introducing |
Can you point to specific examples? |
Zig Fmt Errol/index.zig: Line: 581 ...
const a = u32(value / kTen16); // 1 to 1844
value %= kTen16;
if (a < 10) {
buffer[buf_index] = '0' + u8(a); // THIS LINE: 'a' is a 32 bit integer
buf_index += 1;
} else if (a < 100) {
const i: u32 = a << 1;
buffer[buf_index] = c_digits_lut[i];
buf_index += 1;
buffer[buf_index] = c_digits_lut[i + 1];
buf_index += 1;
} else if (a < 1000) {
buffer[buf_index] = '0' + u8(a / 100); // THIS LINE: 'a' is a 32 bit integer
buf_index += 1;
const i: u32 = (a % 100) << 1; // needed as 32 here
buffer[buf_index] = c_digits_lut[i];
buf_index += 1;
buffer[buf_index] = c_digits_lut[i + 1];
buf_index += 1;
... This was one I found by searching for it, I also have come across cases; I'm sure that @Hejsil (maybe) also has come across cases with handling a rom randomizer. Really any program low level benefits from explicit casting. Removing it effects embedded development I feel (from my somewhat limited experience sure). |
Actually, my code has very few explicit casts. What I use most is slice widening casts ( I think removing the current explicit casts in favor of builtins has a couple of advantages:
|
I'm open to this code being silly, but this my most significant use of the feature, casting back and forth between ints and floats with a single syntax:
|
I presume those casts would stay, it would just be explicit casts between stuff like |
@raulgrell we would add
(this example assumes #683) |
Why not just @cast(T, val)? only one function to remember and easy to grep for in a program, |
* add `@intCast` * add `@floatCast` * add `@floatToInt` * add `@intToFloat` See #1061
Right now there are various builtin functions which can do cast operations, for example:
@ptrCast
@bitCast
@truncate
@intToPtr
These have varying levels of unsafety associated with them, and they have very specific, clearly communicated intentions. They're here to stay.
We also have another concept of implicit casting vs explicit casting. For example, implicit casts:
Implicit casts are the preferred way to convert between types because:
In the above code, there would be a compile error because u8 does not cover the range of u32.
Explicit casts:
Explicit casts are when a type is invoked as a function call. In many cases this has the same behavior as implicit casts, however, sometimes they "force" a conversion. For example, in the above code, the explicit cast overrides the compile error that an implicit cast would give and instead adds a runtime safety check, which in this case will crash the program when run in debug mode.
I propose to remove this "explicit cast" override, instead adding builtin functions for the various kinds of casting that one might want to do. For example, the above code would use a new builtin called
@intShorten
. Using a type as a function call will then serve as only a syntax convenience to perform an "implicit cast".An optional addendum to this proposal would be to remove the syntax completely, forcing zig programmers to utilize variable declarations in order to achieve implicit casts.
The text was updated successfully, but these errors were encountered: