Skip to content

Instantly share code, notes, and snippets.

@goral09
Last active August 29, 2015 14:25
Show Gist options
  • Save goral09/618448bba6ccbd480cdd to your computer and use it in GitHub Desktop.
Save goral09/618448bba6ccbd480cdd to your computer and use it in GitHub Desktop.
class Animal(name: String, size: Int)
object Animal {
def apply(name: String, size: Int) = new Animal(name, size)
def unapply(a: Animal): Option[(String, Int)] = Option((a.name, a.size))
}
def myHumorToday: Animal = Animal("Dog", scala.util.Random.nextInt(50))
def depsOnMyHumor(a: Animal) = a match {
case myHumorToday => "you got lucky boy!"
case _ => "not today"
}
/*<console>:14: warning: patterns after a variable pattern cannot match (SLS 8.1.1)
case myHumorToday => "you got lucky boy!"
^
<console>:15: warning: unreachable code due to variable pattern 'myHumorToday' on line 14
case _ => "not today"
^
<console>:15: warning: unreachable code
case _ => "not today"*/
scala> depsOnMyHumor(Animal("Dog", 20))
res17: String = you got lucky boy!
scala> depsOnMyHumor(Animal("Dog", 20))
res18: String = you got lucky boy!
scala> depsOnMyHumor(Animal("Dog", 20))
res19: String = you got lucky boy!
scala> depsOnMyHumor(Animal("Dog", 20))
res20: String = you got lucky boy!
scala> depsOnMyHumor(Animal("Dog", 20))
res21: String = you got lucky boy!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment