Skip to content

Commit 67c8153

Browse files
authored
Add destructuring pattern match example to enum docs (swiftlang#381)
1 parent 73d7882 commit 67c8153

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ Vehicle vehicle = ...;
209209
switch (vehicle.getDiscriminator()) {
210210
case BICYCLE:
211211
System.out.println("I am a bicycle!");
212-
break
212+
break;
213213
case CAR:
214214
System.out.println("I am a car!");
215-
break
215+
break;
216216
}
217217
```
218218
If you also want access to the associated values, you have various options
@@ -223,12 +223,24 @@ Vehicle vehicle = ...;
223223
switch (vehicle.getCase()) {
224224
case Vehicle.Bicycle b:
225225
System.out.println("Bicycle maker: " + b.maker());
226-
break
226+
break;
227227
case Vehicle.Car c:
228228
System.out.println("Car: " + c.arg0());
229-
break
229+
break;
230230
}
231231
```
232+
or even, destructuring the records in the switch statement's pattern match directly:
233+
```java
234+
Vehicle vehicle = ...;
235+
switch (vehicle.getCase()) {
236+
case Vehicle.Car(var name, var unused):
237+
System.out.println("Car: " + name);
238+
break;
239+
default:
240+
break;
241+
}
242+
```
243+
232244
For Java 16+ you can use [pattern matching for instanceof](https://openjdk.org/jeps/394)
233245
```java
234246
Vehicle vehicle = ...;

0 commit comments

Comments
 (0)