3 ms·
> If there are no implicit conversions, users complain about having to write "1.0" instead of "1", or worse, having to write "1.0L". If there are too many impli
by kbenson 8y ago
> If there are no implicit conversions, users complain about having to write "1.0" instead of "1", or worse, having to write "1.0L". If there are too many implicit conversions, you get unexpected semantics.
There is another way out of this, which is using different operators. Modern Unicode can help greatly with this, but even before that was common, Perl was doing something similar between some of its major types (numbers and strings). Since Perl types are dynamic, and may be used as a string or a number, there would be a lot of ambiguity on comparing two variables for equivalence if the comparison depended on the type. To solve this, Perl has two sets of operators, one for dealing strings (eq,ne,gt,lt,gte,lte, '.' for concatenation), and one for dealing with numbers (==,!=,<,>,<=,>=, + for addition). What these primarily do is interpret both sides as the desired type before the comparison. This greatly reduces ambiguity, but does require more operators be internalized before language proficiency is achieved.
This is of course much harder to accomplish and becomes much more complicated as you increase the number of types. That said, it's an important concept and could help quite a bit in circumstances where things can be ambiguous (for example, your Python example).
- lomnakkus 8y agoAnother way would be to use return type polymorphism. That way you could have a trait similar to Haskell's "Num a" + "Integral a" with "fromIntegral :: a" and just have the parser insert a "fromIntegral" in front of every numeric literal. I don't know much about Rust, but I'd assume from the discussion that it doesn't support this? Of course, Haskell's Num is a horribly broken typeclass, so don't do exactly that. :)