Skip to content

Instantly share code, notes, and snippets.

@adamretter
Created August 11, 2015 22:38
Show Gist options
  • Save adamretter/3bf071e20e62ec63484e to your computer and use it in GitHub Desktop.
Save adamretter/3bf071e20e62ec63484e to your computer and use it in GitHub Desktop.
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"
}
}
```
@adamretter
Copy link
Author

Now results in the compiler error:

[error] /tmp/TestUnapply.scala:18: Parameter type in structural refinement may not refer to an abstract type defined outside that refinement
[error]   def process[T <: Thing: ClassTag](thing: Thing, extract: { def unapply(thing: T): Option[String]}) = thing match {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment