Skip to content

Commit

Permalink
Add void whitelist item for switch expression case (#3026)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
eernstg authored May 2, 2023
1 parent 8f55791 commit 6a6c6c4
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions accepted/future-releases/0546-patterns/feature-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,20 @@ issue an error since the empty placeholder switch is no longer exhaustive,
allowing the user to find all the places in the code that need to be updated to
handle the new subtype.*

We add the following item to the list of [locations where it is not an error
to have an expression of type `void`][void allowlist]:

[void allowlist]:https://github.com/dart-lang/language/blob/f58f8c5d24eed253db41474a3149f7b79a274c63/specification/dartLangSpec.tex#L21981

- In a `<switchExpressionCase>` of the form `p => e`, `e` may have type `void`.

*This means that we treat switch expressions similarly to conditional
expressions with respect to the type `void`. When one or more cases in a switch
expression have type `void`, the switch expression as a whole will have type
`void`, which means that it can in turn only occur in a location which allows
having type `void`. For example,
`void f() => switch (null) { null => print('Got yer!') };` is OK.*

### Variables and scope

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

0 comments on commit 6a6c6c4

Please sign in to comment.