Skip to content

Instantly share code, notes, and snippets.

@NotBobTheBuilder
Last active May 5, 2019 10:17
Show Gist options
  • Save NotBobTheBuilder/1a0617789a42cd0ec8a53ecf2f417b37 to your computer and use it in GitHub Desktop.
Save NotBobTheBuilder/1a0617789a42cd0ec8a53ecf2f417b37 to your computer and use it in GitHub Desktop.
trait TesterTrait {
type Out
def unapply(s: String): Out
}
object Tester extends TesterTrait {
override type Out = Boolean
def unapply(s: String) = false
}
object Matcher1 extends TesterTrait {
override type Out = Option[String]
def unapply(s: String) = Some("1")
}
object Matcher2 extends TesterTrait {
override type Out = Option[(String, String)]
def unapply(s: String) = Some("1" -> "2")
}
object R {
"" match {
case Tester() => true
case Matcher1(m) => true
case Matcher2(a, b) => true
}
}
trait Unapply[Reg] {
type Out
def apply(in: String): Out
}
trait TesterTrait[T] {
def unapply(s: String)(implicit stu: Unapply[T]): stu.Out = stu(s)
}
object Tester extends TesterTrait[Boolean]
object Matcher1 extends TesterTrait[String]
object R {
implicit def stringToBool: Unapply[Boolean] = new Unapply[Boolean] {
type Out = Boolean
def apply(in: String): Out = false
}
implicit def stringToOpt: Unapply[String] =
new Unapply[String] {
type Out = Option[String]
override def apply(in: String): Out = Some("X")
}
"" match {
case Tester() => true
case Matcher1(m) => true
}
}
trait Unapply[Reg] {
type Out
def apply(in: String): Out
}
object Unapply {
type Aux[I, O] = Unapply[I] { type Out = O }
}
trait TesterTrait[T] {
def unapply[O](s: String)(implicit stu: Unapply.Aux[T, O]): stu.Out = stu(s)
}
object Tester extends TesterTrait[Boolean]
object Matcher1 extends TesterTrait[String]
object R {
implicit def stringToBool: Unapply.Aux[Boolean, Boolean] =
new Unapply[Boolean] {
type Out = Boolean
def apply(in: String): Out = false
}
implicit def stringToOpt: Unapply.Aux[String, Option[String]] =
new Unapply[String] {
type Out = Option[String]
override def apply(in: String): Out = Some("X")
}
"" match {
case Tester() => true
case Matcher1(m) => true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment