Pattern Matching for Option Types
Pattern matching on Slang option
types is straightforward as illustrated by the examples below. Remember to put parentheses “()” after “None” (i.e., write “None()”) to distinguish Slang’s option from Scala’s.
// match case - option
@pure def matchOption(o: Option[Z]): String = {
o match { // pattern match on option value
case Some(n) => n.toString()
case None() => "" // must say "None()" to distinguish from Slang from Scala "None".
// case None = "" // ERROR: this is valid Scala, but it produces a Slang error
// because the Slang compiler sees it as Scala option instead
// of a Slang option (Scala options and Slang options
// are different types, and the Scala option is not in the Slang
// subset.)
}
}
// match case - option with nested pairs
@pure def matchOptionNested(o: Option[(Z,Z)]): String = {
o match { // pattern match on option value
case Some((10,n)) => n.toString()
case Some((n,10)) => n.toString()
case Some((20,_)) => "20"
case None() => "" // must say "None()" to distinguish from Slang from Scala "None".
case _ => "Unmatched"
}
}