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
Hello. I like to suggests a discussion about a FR to make possible to
inline switch, as an alternative to nested inline conditionals.
<?php
$value = (function($x) {
switch ($x) {
case 1:
return 'number 1';
case 2:
return 'number 2';
default:
return 'dunno';
}
})(1);
echo $value;
Depends on how many times you need to call it if this works for you or not.
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 david.proweb@gmail.com
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
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.brzuchalski@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-improvementCheers,
Michał Brzuchalskiśr., 16 paź 2019, 03:46 użytkownik David Rodrigues <david.proweb@gmail.com
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
Hi Kosit,
śr., 16 paź 2019 o 09:41 Kosit Supanyo webdevxp.com@gmail.com napisał(a):
Hi Michal
I'had the idea similar to your RFC but instead of using
switch
keyword I
think introducingwhen
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 thanswitch
. 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 ifswitch
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
I mean separation between cases not case conditions. Examples in your RFC
uses semicolon as case separator instead of comma.
<?php
$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:
<?php
$say = switch (date("w")) {
case 0 => "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.brzuchalski@gmail.com> wrote:
Hi Kosit,
śr., 16 paź 2019 o 09:41 Kosit Supanyo webdevxp.com@gmail.com
napisał(a):Hi Michal
I'had the idea similar to your RFC but instead of using
switch
keyword
I think introducingwhen
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 thanswitch
. 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 ifswitch
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'sThis 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
On Wed, Oct 16, 2019 at 4:37 AM Kosit Supanyo webdevxp.com@gmail.com
wrote:
I mean separation between cases not case conditions. Examples in your RFC
uses semicolon as case separator instead of comma.<?php
$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:<?php
$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.brzuchalski@gmail.com> wrote:Hi Kosit,
śr., 16 paź 2019 o 09:41 Kosit Supanyo webdevxp.com@gmail.com
napisał(a):Hi Michal
I'had the idea similar to your RFC but instead of using
switch
keyword
I think introducingwhen
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 thanswitch
. 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 ifswitch
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'sThis 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
chasepeeler@gmail.com
<?php
$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
$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]
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 robehickman@gmail.com
wrote:
<?php
$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}";
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-improvementCheers,
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.
- 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.
- 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
Am 16.10.2019 um 03:46 schrieb David Rodrigues david.proweb@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
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]