Skip to content

Instantly share code, notes, and snippets.

@milessabin
Created May 3, 2015 16:48
Show Gist options
  • Save milessabin/ec45f1ada07d3d3029b8 to your computer and use it in GitHub Desktop.
Save milessabin/ec45f1ada07d3d3029b8 to your computer and use it in GitHub Desktop.
Fish or fowl?
trait Pet {
type This <: Pet
def name: String
def renamed(newName: String): This
}
case class Fish(name: String, age: Int) extends Pet {
type This = Fish
def renamed(newName: String): This = copy(name = newName)
}
case class Chicken(name: String) extends Pet {
type This = Fish
def renamed(newName: String): This = Fish(newName, 3)
}
object FishOrFowl {
def esquire[A <: Pet](a: A): a.This = a.renamed(a.name + ", Esq.")
val f: Fish = esquire(new Chicken("bob")) // Oops!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment