On 01/22/2015 05:39 PM, Andrea Faulds wrote:
Hi Larry,
On 22 Jan 2015, at 22:48, Larry Garfield <la...@garfieldtech.com> wrote:
This looks potentially quite useful, especially for multi-stage comparisons as
you note. Mainly it would mean I don't have to remember which direction is
positive or negative, as I can never get that right without looking it up. :-)
Glad you like it. I, too, sometimes have trouble remembering what direction it
goes in.
The examples say nothing about mixing types, though. Eg, what would these
return:
return 0 <=> "0"
return "" <=> 0
return 1 <=> [1, 2, 3]
Ignoring object property names and going by the order of the property
definition seems like asking for trouble, too.
There may be easy logical answers to the above given PHP's existing type
juggling, but they should be called out explicitly in both the RFC and in any
subsequent documentation.
The behaviour would be the same as the current <, <=, ==, >= and > operators. If >
currently returns TRUE, then it is 1, if == currently returns TRUE, then it is 0, and if < currently
returns TRUE, then it is -1. Despite all the ridiculousness of our existing comparison rules,
apparently <, == and > returning TRUE are mutually exclusive, thank goodness.
I don’t really want to cover this behaviour in the RFC, because explaining it in full
is a considerable effort and the rules it uses are not new, they are the ones PHP
already uses. The behaviour for arrays is perhaps a little odd, but that is our
existing behaviour, and I’d rather not introduce inconsistency by making <=>
use different rules to the other operators.
I agree, however, that documentation for this feature should mention the
behaviour, possibly just linking to our existing documentation on type juggling.
Thanks.
--
Andrea Faulds
http://ajf.me/
Assuming it's accurate and I'm understanding you correctly, I think the
following would be a sufficient statement for the RFC:
-----
The behavior of this operator with mixed types is such that the
following two snippets produce the exact same result in all cases:
return $a <=> $b;
if ($a > $b) {
return -1;
}
elseif ($a < $b) {
return 1;
}
else {
return 0;
}
-----
Or maybe that's implicit in the existing link, I'm not sure. (Now that
I read it over again.) Basically, in order to understand the more
esoteric type juggling that may occur you'd need to mentally expand it
to its "uncompressed form", for which the rules are already defined (if
possibly quirky in some cases).
--Larry Garfield
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php