Re: Re: [DISCUSSION] Match expression
| From: | Dan Ackroyd | Date: | Thu, 23 Apr 2020 11:43:32 +0000 |
| Subject: | Re: Re: [DISCUSSION] Match expression | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-109807@lists.php.net to get a copy of this message | ||
On Mon, 20 Apr 2020 at 19:20, Ilija Tovilo <tovilo.ilija@gmail.com> wrote:
> Just a heads up, I'd like to start the voting on the match expression
> RFC in a couple of days.
> The jury is still out on:
>
> <*multiple issues>
>
> If you have anything new to add to the discussion, this is your chance!
>
On Mon, 20 Apr 2020 at 19:20, Ilija Tovilo <tovilo.ilija@gmail.com> wrote:
> Just a heads up, I'd like to start the voting on the match expression
> RFC in a couple of days.
> The jury is still out on:
>
> <*multiple issues>
>
> If you have anything new to add to the discussion, this is your chance!
>
In the tests there is one test for "Test warning on duplicate match
conditions". I can't see that mentioned in the RFC. Is that going to
be applied? To me, that sounds more like something that should be done
at a static analysis level as:
* Sometimes it's convenient to have redundant match conditions. e.g.
within generated code, as otherwise you would have to eliminate
duplicate conditions.
* I don't think PHP can guarantee to detect duplicate conditions.
Would the duplicate condition* below be detected?
Are the match conditions guaranteed to be called in order that they
are defined? I'm guessing yes, but it doesn't appear to be mentioned.
I would prefer having an explicit fall-through statement, for the
relatively rare cases when fall-through is needed. Is there a strong
reason to not add it now?
Is there any protection against people doing odd stuff with
references*, or is that their own problem if they choose to do that?
cheers
Dan
Ack
//* Duplicate condition
function in_range(int $value, int $min, int $max) {
if ($value < $min) {return $false;}
if ($value > $max) {return $false;}
return true;
}
$value = rand(5, 20);
match (true) {
in_range($value, 10, 15) => 'in range',
in_range($value, 10, 15) => 'in range',
default => 'out of range'
}
//* odd stuff with references
function why_do_this(&$value) {
$oldValue = $value;
$value = rand(0, 10);
if ($oldValue < $newValue) {
return true;
}
return false;
}
$value = rand(5, 20);
match ($value) {
why_do_this($value) => 'foo',
why_do_this($value) => 'bar',
7 => 'lucky number'
default => 'not sure why anyone would do this'
}