Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
> On Oct 15, 2019, at 11:33 PM, Michał Brzuchalski > wrote: > > I have an RFC in draft for that and planning to finish it soon together > with some syntax optimisations - you can see it at > https://wiki.php.net/rfc/switch-expression-and-statement-improvement > > Cheers, > Michał Brzuchalski In an earlier message I said I would return to comment on some concerns I have with the RFC. In general, I would appreciate having a way to express a "switch" in an expression much like how you can express an "if" with the ternary operator. However, the specifics of your suggestion give me pause. Also, some of my concerns are contradictory to each other which I will admit in advance. 1. I am a strong believer in language design that facilitates refactoring. But your syntax would make refactoring from an existing switch harder than it would need to be if the syntax more closely matched the existing switch. In your proposal you replace colons (:) with fat arrows (=>) which has no perceptible benefit, at least not for me: $say = switch (date("w")):string { case 0 => "weekend!"; case 1, 2, 3, 4, 5 => "weekday :("; case 6 => "weekend!"; }; The same logic in today's PHP: switch (date("w")) { case 0: $say = "weekend!"; break case 1, 2, 3, 4, 5: $say = "weekday :("; break case 6: $say = "weekend!"; }; It would be easier to manually refactor if mostly the same syntax was used, e.g. with colons (") vs. fat arrows (=>): $say = switch (date("w")):string { case 0: "weekend!"; case 1, 2, 3, 4, 5: "weekday :("; case 6: "weekend!"; }; So if I had a vote I would vote likely against your RFC with the fat arrows but for it if you changed it to use colons instead. 2. However, I find the above more verbose than would be ideal, and I would like an optional equivalent of the ternary operator, e.g. both a verbose option and a terse option. I said optional above because — unlike the stated preferences of others here on this list — I celebrate having multiple ways to accomplish the same task because some approaches fit a given use-case better than other approaches. What might a terse option look like? Maybe this, although I might have missed conflicts to the existing language: $say = date("w"):string ??? 0: "weekend!"; 1, 2, 3, 4, 5: "weekday; :("; 6: "weekend!"; $default; I will say, one huge benefit to an inline switch would be allowing for switch logic without requiring a `break` (or an explicit `fallthrough`) and without any backward compatibility concerns, at least for some use-cases. -Mike
Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
On Wed, 16 Oct 2019 at 18:10, Bob Weinand wrote: > what's the concrete advantage of this syntax as opposed to the already > possible: > > $value = [ > A1 => A2, > B1 => B2, > ][expr()] ?? C; > What's the concrete advantage of switch over if-elseif? What's the concrete advantage of if blocks over conditional jumps? The primary answer in all three cases is surely "expressiveness". If I came upon code like that, it would take me a while to understand its intent - does $value end up as an array, or something else? what's the ?? there for, and when will expression C be used? Now, it may be that the task in question isn't common enough to deserve its own syntax, or you may not like the proposed syntax, but I think that's very different from coming up with lots of clever ways of writing something, and requiring new syntax to have a "concrete advantage" over each one. Regards, -- Rowan Tommins [IMSoP]
Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
> On Oct 16, 2019, at 5:21 PM, Bob Weinand wrote: > what's the concrete advantage of this syntax as opposed to the already > possible: > > $value = [ > A1 => A2, > B1 => B2, > ][expr()] ?? C; Speaking specifically to your example, it does not handle multiple cases. IOW, you cannot do this: $value = [ A1 => A2, B1,C1, D1 => BCD2, E1 => E2, ][expr()] ?? F; A lesser concern is it must allocate memory for the array simply to implement the switch. Given that I would almost always avoid using that approach because I assume it is always best not to allocate memory unless really needed. Even if allocating the memory is not a problem, I expect other developers might have aversion to that technique based on similar assumptions. -Mike P.S. I am not sold on the proposal as is; I plan a follow up later with my concerns.
Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
Am 16.10.2019 um 21:19 schrieb Claude Pache mailto:claude.pa...@gmail.com>>: Le 16 oct. 2019 à 19:11, Bob Weinand mailto:bobw...@hotmail.com>> a écrit : Am 16.10.2019 um 03:46 schrieb David Rodrigues mailto:david.pro...@gmail.com>>: Hello. I like to suggests a discussion about a FR to make possible to inline switch, as an alternative to nested inline conditionals. $value = switch (expr()) { case A1: return A2; case B1: return B2; default: return C; } Instead of: $expr = expr(); $value = $expr == A1 ? A2 : ( $expr == B1 ? B2 : C ); Just a discussion to check what do you think. -- David Rodrigues Hey David, what's the concrete advantage of this syntax as opposed to the already possible: $value = [ A1 => A2, B1 => B2, ][expr()] ?? C; there are only differences in edge cases (with floats or with type mixing) … but for the vast majority of use cases, it is just as powerful as what you proposed. Bob One concrete advantage is, when A2 or B2 are complex expressions that are costly to evaluate or that throw error when given inappropriate input, they are not evaluated needlessly. —Claude Most switch values aren't expensive though. There's always a possibility to prefix with fn() =>: $value = ([ A1 => fn() => A2, B1 => fn() => B2, ][expr()] ?? fn() => C)(); For the normal case, this is unneeded though (as in, most of my use cases for "inline switches" are typically with string operations). Your mileage may vary. Bob
Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
> Le 16 oct. 2019 à 19:11, Bob Weinand a écrit : > > >> >> Am 16.10.2019 um 03:46 schrieb David Rodrigues : >> >> Hello. I like to suggests a discussion about a FR to make possible to >> inline switch, as an alternative to nested inline conditionals. >> >> $value = switch (expr()) { >> case A1: return A2; >> case B1: return B2; >> default: return C; >> } >> >> Instead of: >> >> $expr = expr(); >> $value = $expr == A1 ? A2 : ( $expr == B1 ? B2 : C ); >> >> Just a discussion to check what do you think. >> >> -- >> David Rodrigues > > Hey David, > > what's the concrete advantage of this syntax as opposed to the already > possible: > > $value = [ >A1 => A2, >B1 => B2, > ][expr()] ?? C; > > there are only differences in edge cases (with floats or with type mixing) … > but for the vast majority of use cases, it is just as powerful as what you > proposed. > > Bob One concrete advantage is, when A2 or B2 are complex expressions that are costly to evaluate or that throw error when given inappropriate input, they are not evaluated needlessly. —Claude -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
> Am 16.10.2019 um 03:46 schrieb David Rodrigues : > > Hello. I like to suggests a discussion about a FR to make possible to > inline switch, as an alternative to nested inline conditionals. > > $value = switch (expr()) { >case A1: return A2; >case B1: return B2; >default: return C; > } > > Instead of: > > $expr = expr(); > $value = $expr == A1 ? A2 : ( $expr == B1 ? B2 : C ); > > Just a discussion to check what do you think. > > -- > David Rodrigues Hey David, what's the concrete advantage of this syntax as opposed to the already possible: $value = [ A1 => A2, B1 => B2, ][expr()] ?? C; there are only differences in edge cases (with floats or with type mixing) … but for the vast majority of use cases, it is just as powerful as what you proposed. Bob -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
Hi Robert That is impossible to implement because there's no way to tell the parser that `switch` is statement or expression and it conflicts with `=>` in array key expression (that's why PHP chose to have arrow function with `fn` prefixed). And IMO your idea is harder to read than normal assignment Regards On Wed, Oct 16, 2019 at 8:58 PM Robert Hickman wrote: > > > > > $say = switch (date("w")) { > > > case 0 => "weekend!"; > > > case 1, 2, 3, 4, 5 => "weekday :("; > > > case 6 => "weekend!"; > > > }; > > > echo "Today is {$say}"; > > If you had a really long expression, it may be easier to read if the > assignment was moved to the end, perhaps like this: > > switch (date("w")) { > case 0 => "weekend!"; > case 1, 2, 3, 4, 5 => "weekday :("; > case 6 => "weekend!"; > // lots more cases ... > } => $say; > > echo "Today is {$say}"; >
Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
On Wed, 16 Oct 2019 at 14:58, Robert Hickman wrote: > > > > > $say = switch (date("w")) { > > > case 0 => "weekend!"; > > > case 1, 2, 3, 4, 5 => "weekday :("; > > > case 6 => "weekend!"; > > > }; > > > echo "Today is {$say}"; > > If you had a really long expression, it may be easier to read if the > assignment was moved to the end, perhaps like this: > > switch (date("w")) { > case 0 => "weekend!"; > case 1, 2, 3, 4, 5 => "weekday :("; > case 6 => "weekend!"; > // lots more cases ... > } => $say; > > echo "Today is {$say}"; > The assignment is not part of the switch syntax, the proposal is for a switch expression which evaluates to a value, and can be used anywhere a value is wanted. Your example is no different from a complex function call: $say = doSomething( $foo, 'weekend', [ 'blah => 42, 'bob' => 'smith' ], moreStuff() ); It might be nice if there was a left-to-right assignment operator (there are some languages which write it that way around), but that should be a separate feature, where you could also write this: doSomething( $foo, 'weekend', [ 'blah => 42, 'bob' => 'smith' ], moreStuff() ) => $say; Regards, -- Rowan Tommins [IMSoP]
Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
> > > $say = switch (date("w")) { > > case 0 => "weekend!"; > > case 1, 2, 3, 4, 5 => "weekday :("; > > case 6 => "weekend!"; > > }; > > echo "Today is {$say}"; If you had a really long expression, it may be easier to read if the assignment was moved to the end, perhaps like this: switch (date("w")) { case 0 => "weekend!"; case 1, 2, 3, 4, 5 => "weekday :("; case 6 => "weekend!"; // lots more cases ... } => $say; echo "Today is {$say}"; -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
On Wed, Oct 16, 2019 at 4:37 AM Kosit Supanyo wrote: > I mean separation between cases not case conditions. Examples in your RFC > uses semicolon as case separator instead of comma. > > $say = switch (date("w")) { > case 0 => "weekend!"; > case 1, 2, 3, 4, 5 => "weekday :("; > case 6 => "weekend!"; > }; > echo "Today is {$say}"; > > I prefer comma because semicolon should only be used to separate statements > but switch expression is list of condition/expression pairs not statements. > So it should be: > > $say = switch (date("w")) { > case 0 => "weekend!", > case 1, 2, 3, 4, 5 => "weekday :(", > case 6 => "weekend!", > }; > echo "Today is {$say}"; > > While using colons and semicolons would better sync up with current switch syntax, I definitely like using arrows and commas better. My only issue* is the fact that this introduces multiple ways to do the same thing, which I thought was confusing and bad for new developers *Not really an issue, just being a bit snarky. I actually love proposals like this. It adds something useful to the language, people that don't like it don't have to use it, and the positives outweigh the negatives in terms of BC breaks (which are none, in this case, making it really easy to outweigh them). You may think t's fine because Java uses semicolon but IMO Java had made a > mistake. (C# is semantically correct) > > Cheers > > On Wed, Oct 16, 2019 at 2:54 PM Michał Brzuchalski < > michal.brzuchal...@gmail.com> wrote: > > > Hi Kosit, > > > > śr., 16 paź 2019 o 09:41 Kosit Supanyo > > napisał(a): > > > >> Hi Michal > >> > >> I'had the idea similar to your RFC but instead of using `switch` keyword > >> I think introducing `when` keyword is better. (as I know a language that > >> uses this keyword is Kotlin) > >> > >> $result = when ($v) { > >> 'foo' => 1, > >> 'bar' => 2, > >> 'x', 'y', 'z' => 3, > >> default => null, > >> }; > >> > >> Above you can see that `when` syntax is more semantic than `switch`. And > >> it is easier to implement in parser because it has no > statement/expression > >> ambiguity. > >> > >> But this might not be preferred by BC camps because introducing a new > >> keyword might break BC. > >> And if `switch` is chosen I still think the syntax should be comma > >> separated instead of semicolon separated for consistency with other list > >> expressions (array/function call). > >> > >> $result = switch ($v) { > >> case 'foo' => 1, > >> case 'bar' => 2, > >> case 'x', 'y', 'x' => 3, > >> default => null, > >> }; > >> > > > > That's exactly my proposal with commas, see here: > > > https://wiki.php.net/rfc/switch-expression-and-statement-improvement#switch_expression_introduction > > Unfortunately, this RFC needs more work cause it mixes switch statement > > enhancement with comma-separated list syntax and of switch statement - I > > need to split it into two RFC's > > > > This is nice hearing that this idea has an interest. > > As soon as the RFC will be split and finished I can start a discussion > > thread. > > > > Cheers, > > Michał Brzuchalski > > > -- Chase Peeler chasepee...@gmail.com
Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
I mean separation between cases not case conditions. Examples in your RFC uses semicolon as case separator instead of comma. "weekend!"; case 1, 2, 3, 4, 5 => "weekday :("; case 6 => "weekend!"; }; echo "Today is {$say}"; I prefer comma because semicolon should only be used to separate statements but switch expression is list of condition/expression pairs not statements. So it should be: "weekend!", case 1, 2, 3, 4, 5 => "weekday :(", case 6 => "weekend!", }; echo "Today is {$say}"; You may think t's fine because Java uses semicolon but IMO Java had made a mistake. (C# is semantically correct) Cheers On Wed, Oct 16, 2019 at 2:54 PM Michał Brzuchalski < michal.brzuchal...@gmail.com> wrote: > Hi Kosit, > > śr., 16 paź 2019 o 09:41 Kosit Supanyo > napisał(a): > >> Hi Michal >> >> I'had the idea similar to your RFC but instead of using `switch` keyword >> I think introducing `when` keyword is better. (as I know a language that >> uses this keyword is Kotlin) >> >> $result = when ($v) { >> 'foo' => 1, >> 'bar' => 2, >> 'x', 'y', 'z' => 3, >> default => null, >> }; >> >> Above you can see that `when` syntax is more semantic than `switch`. And >> it is easier to implement in parser because it has no statement/expression >> ambiguity. >> >> But this might not be preferred by BC camps because introducing a new >> keyword might break BC. >> And if `switch` is chosen I still think the syntax should be comma >> separated instead of semicolon separated for consistency with other list >> expressions (array/function call). >> >> $result = switch ($v) { >> case 'foo' => 1, >> case 'bar' => 2, >> case 'x', 'y', 'x' => 3, >> default => null, >> }; >> > > That's exactly my proposal with commas, see here: > https://wiki.php.net/rfc/switch-expression-and-statement-improvement#switch_expression_introduction > Unfortunately, this RFC needs more work cause it mixes switch statement > enhancement with comma-separated list syntax and of switch statement - I > need to split it into two RFC's > > This is nice hearing that this idea has an interest. > As soon as the RFC will be split and finished I can start a discussion > thread. > > Cheers, > Michał Brzuchalski >
Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
Hi Kosit, śr., 16 paź 2019 o 09:41 Kosit Supanyo napisał(a): > Hi Michal > > I'had the idea similar to your RFC but instead of using `switch` keyword I > think introducing `when` keyword is better. (as I know a language that uses > this keyword is Kotlin) > > $result = when ($v) { > 'foo' => 1, > 'bar' => 2, > 'x', 'y', 'z' => 3, > default => null, > }; > > Above you can see that `when` syntax is more semantic than `switch`. And > it is easier to implement in parser because it has no statement/expression > ambiguity. > > But this might not be preferred by BC camps because introducing a new > keyword might break BC. > And if `switch` is chosen I still think the syntax should be comma > separated instead of semicolon separated for consistency with other list > expressions (array/function call). > > $result = switch ($v) { > case 'foo' => 1, > case 'bar' => 2, > case 'x', 'y', 'x' => 3, > default => null, > }; > That's exactly my proposal with commas, see here: https://wiki.php.net/rfc/switch-expression-and-statement-improvement#switch_expression_introduction Unfortunately, this RFC needs more work cause it mixes switch statement enhancement with comma-separated list syntax and of switch statement - I need to split it into two RFC's This is nice hearing that this idea has an interest. As soon as the RFC will be split and finished I can start a discussion thread. Cheers, Michał Brzuchalski
Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
Hi Michal I'had the idea similar to your RFC but instead of using `switch` keyword I think introducing `when` keyword is better. (as I know a language that uses this keyword is Kotlin) $result = when ($v) { 'foo' => 1, 'bar' => 2, 'x', 'y', 'z' => 3, default => null, }; Above you can see that `when` syntax is more semantic than `switch`. And it is easier to implement in parser because it has no statement/expression ambiguity. But this might not be preferred by BC camps because introducing a new keyword might break BC. And if `switch` is chosen I still think the syntax should be comma separated instead of semicolon separated for consistency with other list expressions (array/function call). $result = switch ($v) { case 'foo' => 1, case 'bar' => 2, case 'x', 'y', 'x' => 3, default => null, }; Cheers On Wed, Oct 16, 2019 at 10:33 AM Michał Brzuchalski < michal.brzuchal...@gmail.com> wrote: > Hi David, > > I'm interested in this feature this is called switch expression and is on > my list of RFC to process after Object Initializer RFC. > > I have an RFC in draft for that and planning to finish it soon together > with some syntax optimisations - you can see it at > https://wiki.php.net/rfc/switch-expression-and-statement-improvement > > Cheers, > Michał Brzuchalski > > śr., 16 paź 2019, 03:46 użytkownik David Rodrigues > > napisał: > > > Hello. I like to suggests a discussion about a FR to make possible to > > inline switch, as an alternative to nested inline conditionals. > > > > $value = switch (expr()) { > > case A1: return A2; > > case B1: return B2; > > default: return C; > > } > > > > Instead of: > > > > $expr = expr(); > > $value = $expr == A1 ? A2 : ( $expr == B1 ? B2 : C ); > > > > Just a discussion to check what do you think. > > > > -- > > David Rodrigues > > >
Re: [PHP-DEV] Inline switch as alternative to nested inline conditional
Hi David, I'm interested in this feature this is called switch expression and is on my list of RFC to process after Object Initializer RFC. I have an RFC in draft for that and planning to finish it soon together with some syntax optimisations - you can see it at https://wiki.php.net/rfc/switch-expression-and-statement-improvement Cheers, Michał Brzuchalski śr., 16 paź 2019, 03:46 użytkownik David Rodrigues napisał: > Hello. I like to suggests a discussion about a FR to make possible to > inline switch, as an alternative to nested inline conditionals. > > $value = switch (expr()) { > case A1: return A2; > case B1: return B2; > default: return C; > } > > Instead of: > > $expr = expr(); > $value = $expr == A1 ? A2 : ( $expr == B1 ? B2 : C ); > > Just a discussion to check what do you think. > > -- > David Rodrigues >