Skip to content

Instantly share code, notes, and snippets.

@Jacoby6000
Forked from sam/ListContainsExtractor.scala
Last active October 9, 2015 19:00
Show Gist options
  • Save Jacoby6000/0ab1f041a6c69188ff2a to your computer and use it in GitHub Desktop.
Save Jacoby6000/0ab1f041a6c69188ff2a to your computer and use it in GitHub Desktop.
Trying to come up with an Extractor I can pass input to other than my match object.
import scala.util.Try
case class Contains(test: String)
object ContainsA {
def unapply(obj: Contains): Option[String] = Some(obj.test)
}
object ContainsB {
def unapply(obj: Contains): Option[Int] = Try(obj.test.toInt).toOption
}
val container = Contains("foo")
container match {
case ContainsB(int) => int * 2 // fails, because containsB will return None.
case ContainsA(foo) => foo // matches here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment