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
| def match(x: Int): String = x match { | |
| case 1 => "one" | |
| case 2 => "two" | |
| case _ => "whatever" | |
| } |
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
| sealed abstract class Vegetable | |
| case class Pea(colour: String, isCooked: Boolean) extends Vegetable | |
| case class Eggplant(colour: String, weight: Int) extends Vegetable | |
| case class ChocolateCake(layers: Int, isVegetable: Boolean = true) extends Vegetable |
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
| def eatVegetable(vegetable: Vegetable): String = | |
| vegetable match { | |
| case Pea(colour, isCooked) => | |
| s"Oh great, a ${if (isCooked) "uncooked" else "cooked" } $colour pea." | |
| case Eggplant(colour, _) => | |
| s"No matter how I cook this $colour eggplant, it'll still be the blandest vegetable around." | |
| case ChocolateCake(layers, _) => | |
| s"Yum, a $layers-layer chocolate cake – the best vegetable of all." | |
| } |
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
| def eatVegetable(vegetable: Vegetable): String = | |
| vegetable match { | |
| case ChocolateCake(layers, _) if layers > 3 => | |
| "Now that's what I call a cake - I mean vegetable." | |
| case _ => | |
| "No, thank you." | |
| } |
OlderNewer