Re: [RFC] Interval Comparison
| From: | Andrea Faulds | Date: | Mon, 07 Nov 2016 01:17:45 +0000 |
| Subject: | Re: [RFC] Interval Comparison | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-96751@lists.php.net to get a copy of this message | ||
Hi,
Fleshgrinder wrote:
It is not very readable and repetitive. It would be interesting to
support a more mathematical notation for this:
if (0 < $x < 42) {
echo 'x is in (0, 42)';
}
if (0 <= $x <= 42) {
echo 'x is in [0, 42]';
}
I cannot judge how complicated it is to extend the Bison definitions to
allow this. However, if there is interest I'm sure there is a way. :)
Python supports this, I believe it lets you use arbitrary chains of comparisons.
PHP is in the fortunate position where the associativity of the comparison operators is undefined. That is, 0 < $x < 42 is currently invalid syntax. Some other languages made the (in my view) mistake of defining a precedence here, so that it gets interpreted as `(0 < $x) < 42`, which is (generally?) useless. PHP doesn't have this problem.
I've wanted to do this myself, but figuring out how to implement it was tricky, so I didn't spend much time on it. But it's certainly possible, and I'd appreciate this if it was added.
Thanks for bringing it up!
--
Andrea Faulds
https://ajf.me/