Skip to content

Instantly share code, notes, and snippets.

@chemikadze
Created December 31, 2014 08:33
Show Gist options
  • Save chemikadze/6bf44d676a0e302f724b to your computer and use it in GitHub Desktop.
Save chemikadze/6bf44d676a0e302f724b to your computer and use it in GitHub Desktop.
scala> case class Scalar(s: String)
scala> case class Sequence(s: Seq[String])
scala> object ScalarMatcher { def unapply(x: Scalar) = Some(x.s) }
scala> object SequenceMatcher { def unapply(x: Sequence) = Some(x.s) }
scala> Scalar("x").s match { case ScalarMatcher(x) => x ; case _ => "because fuck you" }
<console>:11: error: scrutinee is incompatible with pattern type;
found : Scalar
required: String
Scalar("x").s match { case ScalarMatcher(x) => x ; case _ => "because fuck you" }
^
scala> Sequence(Seq("x")).s match { case SequenceMatcher(x) => x ; case _ => "because fuck you" }
res6: Object = because fuck you
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment