-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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