Created
May 3, 2015 16:48
-
-
Save milessabin/ec45f1ada07d3d3029b8 to your computer and use it in GitHub Desktop.
Fish or fowl?
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
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