Replies: 5 comments 14 replies
-
I think there should be two questions:
|
Beta Was this translation helpful? Give feedback.
-
Yeah, the entire raison d'etre of char, wchar, and dchar is to differentiate them from integral types. Having them implicitly convert to each other, or worse yet, having a char match an int function overload or an int match a char function overload is just pure bonkers. IMO all char types should not be interconvertible with int types. If you want to be doing arithmetic with ASCII values or Unicode codepoint values (usually a bad idea but sometimes necessary), you should be forced to use ugly Kill it with fire and extreme prejudice, I say. |
Beta Was this translation helpful? Give feedback.
-
I like true+true=2 and true*a=a |
Beta Was this translation helpful? Give feedback.
-
A safer idiomatic variant with runtime safety checks for arithmetic overflows is: int x = 1234;
char y = x.to!char; // and it's shorter than "cast(char)x" In the obvious cases the compiler is able to optimize it out, similar to how it can optimize out some of the array bounds checking. Possibly even an extra compiler option can be introduced to omit overflow checks in such expressions to satisfy the needs of those, who want to trade safety for a little bit of extra performance (effectively complementing the existing |
Beta Was this translation helpful? Give feedback.
-
I'd say just change it. There can even be a justification to force explicit conversion from non lossy conversion. Rust has this and must use as or ::from. let i: u8 = 1;
let u: i32::from(i); I'm a bit unsure if this is taking it too far. We have the chance now to get rid of this and it will help us to avoid bugs. People who like to do arithmetics with bool/chars will lose nothing, just an additional cast to show the intention. |
Beta Was this translation helpful? Give feedback.
-
Another idea rejected by Walter: treat bool as a separate type from int and disable implicit promotion to int. Does this make sense, or are the current int promotion rules better?
Beta Was this translation helpful? Give feedback.
All reactions