Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Do chained comparisons short-circuit?

2 views
Skip to first unread message

Joe Gottman

unread,
Jan 18, 2006, 9:13:58 PM1/18/06
to Perl6
Suppose I have code that looks like this:

my ($x, $y, $z) = (1, 2, 3);

say "sorted backward" if ++$x > ++$y > ++$z;

Will $z be incremented even though the chained comparison is known to be
false after ++$x and ++$y are compared?

Joe Gottman


Luke Palmer

unread,
Jan 18, 2006, 10:25:22 PM1/18/06
to jgot...@carolina.rr.com, Perl6

I don't see a reason for chained comparisons not to short-circuit,
besides the surprise factor. But anyone who knows about &&, and
understands chained comparisons as expanding to &&, should understand
short-circuiting behavior.

Luke

Ph. Marek

unread,
Jan 19, 2006, 1:33:50 AM1/19/06
to perl6-l...@perl.org, Luke Palmer, jgot...@carolina.rr.com
Although that may lead to _longer_ code, which (when extended) is likely to be
broken:

$x++; $y++; $z++;
say "sorted backward" if $x > $y > $z;

To be honest, in this example it mostly doesn't matter; if $x > $y, then
($x+1) > ($y+1). But in many quickly written scripts I did some numeric
operation to force the value to numeric, even if I got a parameter like
"string" (which becomes 0 when numyfied)


How about some flag saying "don't short-circuit this"?


Regards,

Phil

0 new messages