Re:Why don't more languages offer flow typing?
by dragonwriter on Monday April 04, 2022 @03:52PM
(#30911235)
> Unless I'm mistaken, without narrow typing, this cast would be impossible
same sized integer casts in Rusts are no-ops [0]; the conditional isn't type narrowing, it just avoids the cases where the cast would not preserve the same semantic value.
[0] https://doc.rust-lang.org/reference/expressions/operator-exp...
For example, consider this Rust snippet :
Unless I'm mistaken, without narrow typing, this cast would be impossible, and there would be no way to avoid the redundant runtime check caused by try_into().unwrap(), that is not even optimized unless you trick the compiler.