4 ms·
What should 0x40 == 64 equal? Ought hexadecimal integer and decimal integer be the same type? As long as conversion between representations is well-defined and
by msla 4y ago
What should 0x40 == 64 equal? Ought hexadecimal integer and decimal integer be the same type?
As long as conversion between representations is well-defined and not hugely surprising, what does it matter?
- tialaramex 4y ago> As long as conversion between representations is well-defined and not hugely surprising, what does it matter? Surely the MD5 example demonstrates exactly how this ends up being a problem. On the surface "1e5" == 100000 seems like a reasonable conversion between representations and unsurprising, but by silently converting things and then comparing the result we get this astonishing result. Conversions need to be made explicit. This is an opportunity for programmers to consider whether they actually wanted a conversion (the MD5 programmer did not) and for API designers to offer multiple distinct conversions when appropriate. "1e5" == 100000 is only one reasonable interpretation. Maybe it's a lowercase hexadecimal value. If you make the programmer explicitly convert they can choose.
- giantrobot 4y ago> On the surface "1e5" == 100000 Coercing an explicit string e.g. "1e5" to a numeric is boneheaded. It's fine to support different numeric formats like exponent or hex format. Weak typing and type coercion is a major footgun in a language.
- msla 4y ago> Weak typing and type coercion is a major footgun in a language. Type coercion is an expression of strong typing, and it's inevitable if you want a language that's usable. For example, "array of int of length 5" is a type, and "array of int of length 6" is a different type. Do you want to write a separate sort function for each of those types?
- tialaramex 4y agoI don't want to write a separate sort function, the computer can and should do all that work for me automatically. Because Default is tricky to genericise, there literally are 32 distinct implementations provided for Rust's [T; 1] through [T; 32] of Default::default() https://doc.rust-lang.org/src/core/array/mod.rs.html#467 https://doc.rust-lang.org/src/core/array/mod.rs.html#467
- pdpi 4y ago> As long as conversion between representations is well-defined and not hugely surprising, what does it matter? In Javascript: let a = '0'; a == !a; // True. Why? let b = !a; (a == b) == (!a == !b) // False. Why? None of the coercions involved is particularly surprising, but the result most definitely is.
- aflag 4y agoThey are the same type and represent the same number. Just different literals. "ABC" is also the same as "A" + "B" + "C".