[RFC] Interval Comparison

php.internals

Fleshgrinder

9 years ago
Validating whether a number is within a closed or open interval is currently possible only via a construct like the following: ``` if (0 < $x && $x < 42) { echo 'x is in (0, 42)'; } if (0 <= $x && $x <= 42) { echo 'x is in [0, 42]'; } ``` 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. :)
-- Richard "Fleshgrinder" Fussenegger

David Rodrigues

9 years ago
I guess that the biggest problem is about the code parsing. Currently PHP should supports what you wrote, but with another meaning. And personally I think that this feature is not too common on programming language in general, once that it is possible from first format (maybe more clear too). Em 6 de nov de 2016 6:30 PM, "Fleshgrinder" <php@fleshgrinder.com> escreveu:

David Walker

9 years ago
On Sun, Nov 6, 2016 at 1:59 PM David Rodrigues <david.proweb@gmail.com> wrote:
> I guess that the biggest problem is about the code parsing. Currently PHP > should supports what you wrote, but with another meaning. And personally I > think that this feature is not too common on programming language in > general, once that it is possible from first format (maybe more clear too). > > Em 6 de nov de 2016 6:30 PM, "Fleshgrinder" <php@fleshgrinder.com> > escreveu: > > > Validating whether a number is within a closed or open interval is > > currently possible only via a construct like the following: > > > > ``` > > if (0 < $x && $x < 42) { > > echo 'x is in (0, 42)'; > > } > > > > if (0 <= $x && $x <= 42) { > > echo 'x is in [0, 42]'; > > } > > ``` > > > > 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]'; > > } > > ``` > > > >
Richard, I'd be a +1 on this. I dislike having the double comparison all over checking for bounds, and have for quite a while. Akin to what David brought up, https://3v4l.org/7MvlB. It seems the requested syntax is not valid in any PHP version testable here. I can't speak to the complexities of implementing something like that, but would find it useful. Of course, you'd have to worry about other conditions like ``` if (0 < $x > 20) ``` By worry about, I mean, should it be invalid syntax, or, should it be optimized to be `20 < $x`? I'd say you'd have to maintain the gt/lt for chaining. Then I'd wonder about similar syntax to check if 2 variables are both greater than 0, but one greater/equal to the other: ``` if (0 < $x <= $y) ```
-- Dave

Andrew Faulds

9 years ago
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/