Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Text
Posted by: Pahay Ragh
Added: Oct 27, 2020 6:30 PM
Views: 13
Tags: no tags
  1. abstract class Animal
  2. case class Mammal(name: String, fromSea: Boolean) extends Animal
  3. case class Bird(name: String) extends Animal
  4. case class Fish(name: String) extends Animal
  5.  
  6. def caseClassesPatternMatching(animal: Animal): String = {
  7.   animal match {
  8.     case Mammal(name, fromSea) => s"I'm a $name, a kind of mammal. Am I from the sea? $fromSea"
  9.     case Bird(name) => s"I'm a $name, a kind of bird"
  10.     case _ => "I'm an unknown animal"
  11.   }
  12. }