Skip to content

Instantly share code, notes, and snippets.

@adamretter
Created August 11, 2015 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamretter/7aa359c7bc5b1368c368 to your computer and use it in GitHub Desktop.
Save adamretter/7aa359c7bc5b1368c368 to your computer and use it in GitHub Desktop.
Trying to pass unapply types
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"
}
}
@adamretter
Copy link
Author

Compiler error is:

[error] /tmp/proj1/TestUnapply.scala:10: type mismatch;
[error]  found   : AnyRef{def unapply(thing: TestingUnapply.ThingA): Option[String]}
[error]  required: AnyRef{def unapply[T <: TestingUnapply.Thing](thing: T): Option[String]}
[error]   process(x, new { def unapply(thing: ThingA) = ThingA.unapply(thing)})
[error]              ^
[error] /tmp/proj1/TestUnapply.scala:11: type mismatch;
[error]  found   : AnyRef{def unapply(thing: TestingUnapply.ThingB): Option[String]}
[error]  required: AnyRef{def unapply[T <: TestingUnapply.Thing](thing: T): Option[String]}
[error]   process(y, new { def unapply(thing: ThingB) = ThingB.unapply(thing).map(_._2.a)})
[error]              ^

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