Front page | perl.perl5.porters |
Postings from February 2020
Re: chained comparisons
Thread Previous
|
Thread Next
From:
Smylers
Date:
February 5, 2020 12:13
Subject:
Re: chained comparisons
Message ID:
1701542ef81.d769ace3305066.6224765721274965331@stripey.com
Zefram writes:
> Branch zefram/cmpchain at <git://river.fysh.org/zefram/perl.git> may be
> of some interest.
Nice — that's definitely of interest! Thank you.
A few small doc suggestions. Re:
A sequence of relational operators, such as
S<C<"$a E<lt> $b E<lt>= $c">>, does not group in accordance with left
or right associativity, which would produce the almost-useless result
of using the truth-value result of one comparison as a comparand in
another comparison. Instead the comparisons are I<chained>: each
comparison is performed on the two arguments surrounding it, with each
interior argument taking part in two comparisons, and the comparison
results are implicitly ANDed.
• I think for somebody first encountering this feature, it's more useful
to state what it does, before what it doesn't.
Specifically “You can chain operators like $a < $b <$c” is
conceptually quite easy to understand, even for beginners.
Thinking about what that would do were it not for chaining, involving
associativity and the truth value of one comparison being used in
another, isn't necessary in order to be able to use chaining, as well
as being harder to think about.
• The word ‘comparand’ doesn't currently appear anywhere else in Perl's
docs. It's a bit jargony, and may be offputting to people unfamiliar
with the word. I haven't looked to see how comparand's are referred to
elsewhere in the docs, but if there's a reasonably concise alternative
which can be used, that may be better.
And in this bit:
Thus S<C<"$a E<lt> $b E<lt>= $c">> behaves very much like
S<C<"$a E<lt> $b && $b E<lt>= $c">>. The ANDing short-circuits just
like C<"&&"> does, stopping the sequence of comparisons as soon as one
yields false. Each argument expression is evaluated at most once, even
if it takes part in two comparisons.
• “very much like” suggests it isn't identical, but doesn't precisely
state the difference(s). The common expression only being evaluated
once is a difference, but it would be clearer to specifically label it
as such, and also state if that's the only difference or there's
something else than hasn't occurred to me.
It may also be worth giving an example of where avoiding double
evaluation makes a difference, to illustrate that chaining isn't just
a minor syntactic convenience but can have other benefits.
Thank you again for implementing this.
Smylers
Thread Previous
|
Thread Next