On Sat, 28 Mar 2020 at 12:40, Ilija Tovilo <tovilo.il...@gmail.com> wrote:
>
> Hi internals
>
> I'd like to move the RFC switch expression to "under discussion". It
> tries to address some of the common issues with the switch statement
> by introducing a new switch expression.
>
> https://wiki.php.net/rfc/switch_expression


Hi Ilija,


The switch statement is definitely something that has issues. However
I think the RFC as written is not a good starting point.

1) The behaviour of switch with regards to type conversion is the
biggest thing I care about. If the RFC isn't going to touch that, then
it's way less interesting to me.

2) In general, I think that using the switch keyword with slightly
different behaviour is not a good idea. Even if we had 'editions' in
PHP, having subtly different behaviour is very confusing. A better
approach would be to use a new keyword (maybe 'select') that has the
correct behaviour from the start.

3) Fallthrough:

"The fallthrough behavior can't reasonably be changed in the switch
statement because it would break a lot of code. However this RFC
porposes allowing multiple conditions per case so that the intention
of running the same code can be expressed more clearly. "

I don't think this is the right approach. Most new languages have the rules of:

* each 'case' statement has an implicit break statement at the end of that case.
* they provide an explicit 'fallthrough' keyword for where fall
through is desired.

In general I think there are lots of details that need to be thought through.

I don't think this email list is a great place to work through all of
those details. Email is not a great medium for this type of discussion
in general, and causes a lot of noise for people who are not taking
part in that work.

Some of the people who are interested in working on how to implement
generics in PHP have been doing that in a github repo
https://github.com/PHPGenerics/php-generics-rfc/issues

If you have the energy to work on this, please consider opening a
similar repo as a place for people to discuss ideas and work through
all the details, as that would be far more likely to have a productive
discussion and produce the best possible RFC*. oh, and obviously link
to that repo here so people can join that conversation.

cheers
Dan
Ack

* btw it might be worth thinking about pattern matching as well.
Currently the below is supported, but is also horrific coding (imo)
standard.

function checkValue(int $x, bool $errorIfOverTen) {
    switch (true) {

        case $x < 5:
            return "too small\n";

        case $x > 10 && $errorIfOverTen:
            return "value is over ten, which is not allowed.\n";

        default:
            return "Ok\n";
    }
}

echo checkValue(3, false);
echo checkValue(11, false);
echo checkValue(11, true);

output is
// too small
// Ok
// value is over ten, which is not allowed.

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to