Michael Fowler wrote:
> I would argue that you should be manipulating your data, or checking values,
> so that numbers and strings are sorted how you wish. The proposed isnum(),
> or way-to-determine-if-a-scalar-is-a-number would help. This should be an
> explicit check, though, because you have a very definite idea about how you
> want things sorted.
That's not very generic programming is it? People should be able to write
code and only say "this must be ordered", and let Perl figure out what ordered
means. In C++ you have to write an overloaded comparison operator and then
use a template function. Damian is saying Perl should make this hard
thing easier.
I think Glenn Linderman implied that the string comparison is Perl's generic
ordering function. It isn't. Equality is perfect, but ordering isn't. If they
were, the results of lt would be the same as < for all numbers.
"20" lt "3" # true
20 lt 3 # true -- should be false
I don't think it's possible to retrofit different behavior into Perl though,
mostly because it's weakly typed between strings and numbers.
Perhaps the new min() and max() operators should have polymorphic behavior
with the type chosen as the simplest type representation that does not lose
information? eq is already generic like Glenn said.
Here's a proper polymorphic <= (assuming min() is polymorphic):
min($a, $b) eq $a
Ugly, but minimal changes to the language. What might be nicer is to complement
min() and max() with ordered(). ordered() will just return true if all it's
arguments are "well ordered". The same information preserving downcasting
should be used in selecting an ordering function.
- Ken