
damian at conway
Aug 20, 2012, 5:59 PM
Post #6 of 55
(211 views)
Permalink
|
|
Re: Newline legacy (Was: fixing smartmatch just hard enough (and when, too))
[In reply to]
|
|
David Golden commented: > But as to smartmatch -- I don't think I'd want it to be so smart. Or > rather, I want the "smart" to mean allowing a variety of comparator > functions, not smart as in "I'm going to guess how you want to interpret a > value as a string/number/without-whitespace, etc.". Ideally, the numeric case in the smartmatch table would work like this: Any Num numeric comparison $a == $b That is: if the RHS operand is categorically a number, unconditionally do numeric comparison. Then there wouldn't be any guesswork. They explicitly said "match against a number", so we naturally match numerically. And, because 'when X' is (I hope!) becoming uncategorically '$_ ~~ X', then: when 0 {...} would also work correctly, even on unchomped input: they said "match against the number 0", so we match numerically. And if they wanted eq matching for that, they'd write: when "0" {...} The problem is that everyone tells me that's not possible in Perl 5. I'm still not certain why. I *suspect* it's because it's complicated to distinguish the right behaviour for: $num = 0; say $num; say $something ~~ $num; as, after having been used as a string, the contents of $num have both string and number internal representations. If that is indeed the problem, maybe it would be sufficient to make it so that if the RHS operand to a smartmatch has an internal numeric representation, it always uses ==, even if there's also an internal string representation. But I guess that has DWIM problems too: $str = "0"; say 0+$str; say $something ~~ $str; # Now uses == :-( Sigh. Or, perhaps, we could define smartmatch to use == if the RHS operand *only* has a numeric representation (i.e. no internal string representation), in which case you would get: $num = 0; $something ~~ $num # uses == (no internal string rep) $something ~~ "0" # always eq $something ~~ 0 # always == $something ~~ "$num" # always eq $something ~~ $num # now uses eq :-( has internal string rep ) $something ~~ 0+$num # always uses == $something ~~ 2*$num # always uses == $something ~~ length($num) # always uses == # etc. That's much less bad, I feel: most cases then just DWIM. The only issue is that you always need to prefix a variable-as-right-operand with '0+' if you want to ensure that it uses numeric comparison. Would that be a feasible solution? Damian
|