Created
August 11, 2015 22:38
-
-
Save adamretter/3bf071e20e62ec63484e to your computer and use it in GitHub Desktop.
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
Updating the code to: | |
```scala | |
import scala.reflect.ClassTag | |
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[T <: Thing: ClassTag](thing: Thing, extract: { def unapply(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
Now results in the compiler error: