My questions is, "Why would a language designer consider allowing comparison between different data types?". Also, does this make more sense in a functional language?
For example, in erlang one can perform the following comparisons:
%% Tuples are greater than numbers
999999 < {1,2}.
true
%% Lists are greater than tuples
{90000} < [1].
true
%% Atoms are greater than numbers
1 < false.
true
In python 2.x as well,
p = lambda x,y: x+y
p > (1)
True
p < (1)
False
p == (1)
False
Though looks like the python community decided this wasn't a good idea after all:
objects of different types always compare unequal, and are ordered consistently but arbitrarily. [...] This unusual definition of comparison was used to simplify the definition of operations like sorting and the in and not in operators. source
From the Python 3 release note:
The ordering comparison operators (<, <=, >=, >) raise a TypeError exception when the operands don’t have a meaningful natural ordering. Thus, expressions like 1 < '', 0 > None or len <= len are no longer valid, and e.g. None < None raises TypeError instead of returning False. A corollary is that sorting a heterogeneous list no longer makes sense – all the elements must be comparable to each other. source
This kind of explains why, but I was wondering if there are other reasons to allow this, particularly in functional languages.
In dynamic languages it makes a certain amount of sense, as it's nice to be able to sort heterogeneous lists and build heterogeneous trees. And I think I would say that it's not so much functional languages where it's dubious as it is strongly typed languages, for obvious reasons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With