-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Switch expression #5308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
[RFC] Switch expression #5308
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a1b2dc6
Implement switch expression
iluuu1994 77bd887
Allow multiple conditions per switch case
iluuu1994 0fdb273
Add UnhandledSwitchCaseError
iluuu1994 ff7bd2d
Implement pretty printing for switch expression
iluuu1994 2f327a2
Switch to `switch ($x)` syntax
iluuu1994 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
Basic switch expression functionality test | ||
--FILE-- | ||
<?php | ||
|
||
function wordify($x) { | ||
return switch ($x) { | ||
0 => 'Zero', | ||
1 => 'One', | ||
2 => 'Two', | ||
3 => 'Three', | ||
4 => 'Four', | ||
5 => 'Five', | ||
6 => 'Six', | ||
7 => 'Seven', | ||
8 => 'Eight', | ||
9 => 'Nine', | ||
}; | ||
} | ||
|
||
for ($i = 0; $i <= 9; $i++) { | ||
print wordify($i) . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Zero | ||
One | ||
Two | ||
Three | ||
Four | ||
Five | ||
Six | ||
Seven | ||
Eight | ||
Nine |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--TEST-- | ||
Switch expression omit trailing comma | ||
--FILE-- | ||
<?php | ||
|
||
function print_bool($bool) { | ||
echo switch ($bool) { | ||
true => "true\n", | ||
false => "false\n" | ||
}; | ||
} | ||
|
||
print_bool(true); | ||
print_bool(false); | ||
|
||
?> | ||
--EXPECT-- | ||
true | ||
false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--TEST-- | ||
Switch expression default case | ||
--FILE-- | ||
<?php | ||
|
||
function get_value($i) { | ||
return switch ($i) { | ||
1 => 1, | ||
2 => 2, | ||
default => 'default', | ||
}; | ||
} | ||
|
||
echo get_value(0) . "\n"; | ||
echo get_value(1) . "\n"; | ||
echo get_value(2) . "\n"; | ||
echo get_value(3) . "\n"; | ||
|
||
?> | ||
--EXPECT-- | ||
default | ||
1 | ||
2 | ||
default |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--TEST-- | ||
Switch expression with true as expression | ||
--FILE-- | ||
<?php | ||
|
||
function get_range($i) { | ||
return switch (true) { | ||
$i >= 50 => '50+', | ||
$i >= 40 => '40-50', | ||
$i >= 30 => '30-40', | ||
$i >= 20 => '20-30', | ||
$i >= 10 => '10-20', | ||
default => '0-10', | ||
}; | ||
} | ||
|
||
echo get_range(22) . "\n"; | ||
echo get_range(0) . "\n"; | ||
echo get_range(59) . "\n"; | ||
echo get_range(13) . "\n"; | ||
echo get_range(39) . "\n"; | ||
echo get_range(40) . "\n"; | ||
|
||
?> | ||
--EXPECT-- | ||
20-30 | ||
0-10 | ||
50+ | ||
10-20 | ||
30-40 | ||
40-50 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--TEST-- | ||
Switch expression discarding result | ||
--FILE-- | ||
<?php | ||
|
||
switch (1) { | ||
1 => print "Executed\n", | ||
}; | ||
|
||
?> | ||
--EXPECT-- | ||
Executed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--TEST-- | ||
Switch expression with no cases | ||
--FILE-- | ||
<?php | ||
|
||
$x = switch (true) {}; | ||
|
||
?> | ||
--EXPECTF-- | ||
Parse error: syntax error, unexpected '}' in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--TEST-- | ||
Switch expression exception on unhandled case | ||
--FILE-- | ||
<?php | ||
|
||
function get_value($i) { | ||
return switch ($i) { | ||
1 => 1, | ||
2 => 2, | ||
}; | ||
} | ||
|
||
echo get_value(1) . "\n"; | ||
echo get_value(2) . "\n"; | ||
echo get_value(3) . "\n"; | ||
|
||
?> | ||
--EXPECTF-- | ||
1 | ||
2 | ||
|
||
Fatal error: Uncaught UnhandledSwitchCaseError in %s | ||
Stack trace: | ||
#0 %s: get_value(3) | ||
#1 {main} | ||
thrown in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--TEST-- | ||
Switch expression precedence | ||
--FILE-- | ||
<?php | ||
|
||
print switch (!true) { | ||
false => "! has higher precedence\n" | ||
}; | ||
|
||
$throwableInterface = Throwable::class; | ||
print switch (new RuntimeException() instanceof $throwableInterface) { | ||
true => "instanceof has higher precedence\n" | ||
}; | ||
|
||
print switch (10 ** 2) { | ||
100 => "** has higher precedence\n" | ||
}; | ||
|
||
?> | ||
--EXPECT-- | ||
! has higher precedence | ||
instanceof has higher precedence | ||
** has higher precedence |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
Switch expression multiple conditions per case | ||
--FILE-- | ||
<?php | ||
|
||
function is_working_day($day) { | ||
return switch ($day) { | ||
1, 7 => false, | ||
2, 3, 4, 5, 6 => true, | ||
}; | ||
} | ||
|
||
for ($i = 1; $i <= 7; $i++) { | ||
var_dump(is_working_day($i)); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
bool(false) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(false) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--TEST-- | ||
Switch statement multiple conditions per case | ||
--FILE-- | ||
<?php | ||
|
||
function is_working_day($day) { | ||
switch ($day) { | ||
case 1, 7: | ||
return false; | ||
case 2, 3, 4, 5, 6: | ||
return true; | ||
}; | ||
|
||
return null; | ||
} | ||
|
||
for ($i = 0; $i <= 8; $i++) { | ||
var_dump(is_working_day($i)); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
NULL | ||
bool(false) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(false) | ||
NULL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--TEST-- | ||
Pretty printing for switch expression | ||
--FILE-- | ||
<?php | ||
|
||
assert(switch ('foo') { default => false }); | ||
|
||
assert(switch ('foo') { | ||
'foo', 'bar' => false, | ||
'baz' => false, | ||
}); | ||
|
||
assert((function () { | ||
switch ('foo') { | ||
case 'foo', 'bar': | ||
return false; | ||
case 'baz': | ||
return false; | ||
} | ||
})()); | ||
|
||
?> | ||
--EXPECTF-- | ||
Warning: assert(): assert('foo' switch { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This syntax needs to be updated. |
||
default => false, | ||
}) failed in %s on line %d | ||
|
||
Warning: assert(): assert('foo' switch { | ||
'foo', 'bar' => false, | ||
'baz' => false, | ||
}) failed in %s on line %d | ||
|
||
Warning: assert(): assert(function () { | ||
switch ('foo') { | ||
case 'foo', 'bar': | ||
return false; | ||
case 'baz': | ||
return false; | ||
} | ||
}()) failed in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ enum _zend_ast_kind { | |
ZEND_AST_STMT_LIST, | ||
ZEND_AST_IF, | ||
ZEND_AST_SWITCH_LIST, | ||
ZEND_AST_SWITCH_CASE_COND_LIST, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could actually probably reuse the |
||
ZEND_AST_CATCH_LIST, | ||
ZEND_AST_PARAM_LIST, | ||
ZEND_AST_CLOSURE_USES, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is useless now.