Skip to content

Commit 6a6c6c4

Browse files
authored
Add void whitelist item for switch expression case (#3026)
Add void usage whitelist item for switch expression case, that is: `switch (myBool) { true => print('true'), false => print('false') }` is OK and has type `void`.
1 parent 8f55791 commit 6a6c6c4

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

accepted/future-releases/0546-patterns/feature-specification.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,20 @@ issue an error since the empty placeholder switch is no longer exhaustive,
21582158
allowing the user to find all the places in the code that need to be updated to
21592159
handle the new subtype.*
21602160

2161+
We add the following item to the list of [locations where it is not an error
2162+
to have an expression of type `void`][void allowlist]:
2163+
2164+
[void allowlist]:https://github.com/dart-lang/language/blob/f58f8c5d24eed253db41474a3149f7b79a274c63/specification/dartLangSpec.tex#L21981
2165+
2166+
- In a `<switchExpressionCase>` of the form `p => e`, `e` may have type `void`.
2167+
2168+
*This means that we treat switch expressions similarly to conditional
2169+
expressions with respect to the type `void`. When one or more cases in a switch
2170+
expression have type `void`, the switch expression as a whole will have type
2171+
`void`, which means that it can in turn only occur in a location which allows
2172+
having type `void`. For example,
2173+
`void f() => switch (null) { null => print('Got yer!') };` is OK.*
2174+
21612175
### Variables and scope
21622176

21632177
Patterns often exist to bind new variables. The language must ensure that the

0 commit comments

Comments
 (0)