Created
August 11, 2015 22:21
-
-
Save adamretter/7aa359c7bc5b1368c368 to your computer and use it in GitHub Desktop.
Trying to pass unapply types
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
object TestingUnapply { | |
sealed trait Thing | |
case class ThingA(a: String) extends Thing | |
case class ThingB(b: String, thingA: ThingA) extends Thing | |
val x: Thing = ThingA("hello") | |
val y: Thing = ThingB("goodbye", ThingA("maybe")) | |
process(x, new { def unapply(thing: ThingA) = ThingA.unapply(thing)}) | |
process(y, new { def unapply(thing: ThingB) = ThingB.unapply(thing).map(_._2.a)}) | |
def process(thing: Thing, extract: { def unapply[T <: Thing](thing: T): Option[String]}) = thing match { | |
case extract(a) => s"The value of a is: $a" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compiler error is: