Pattern matching with data classes

When you define your data classes using the record keyword, you get the added advantage of the conversion of aggregate and exploded forms of your data class. For example, the following code shows how the switch statement might explode the data:

interface Garment {} 
record Button(float radius, Color color); 
record Shirt(Button button, double price); 
record Trousers(float length, Button button, double price); 
record Cap(..) 
 
switch (garment) { 
     case Shirt(Button(var a1, var a2), Color a3): ... 
     case Trousers(float a1, Button(var a2, var a3), double a4): ... 
     .... 
}

The switch statement can use a data class, without using its exploded form. The following code is also effective:

switch (garment) { 
     case Shirt(Button a1, Color a2): ... 
     case Trousers(float a1, Button a2, double a3): ... 
     .... 
}  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.78.83