Skip to content

Instantly share code, notes, and snippets.

@sam
Created October 9, 2015 18:43
Show Gist options
  • Save sam/17ccbdd10644548ede6b to your computer and use it in GitHub Desktop.
Save sam/17ccbdd10644548ede6b 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.
case class Contains(test: String) {
def unapply(list: List[_]): Boolean = list contains test
}
@sam
Copy link
Author

sam commented Oct 9, 2015

This works for example:

→ scala
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.

scala> class Contains(test: String) {
     | def unapply(list: List[_]): Boolean = list contains test
     | }
defined class Contains

scala> val test = new Contains("bob")
test: Contains = Contains@41d477ed

scala> List("ted", "bob", "fred") match {
     | case test() => println("FOUND BOB")
     | case _ => println("nope")
     | }
FOUND BOB

scala> 

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