scala - strange behavior while pattern matching -
object state { def unapply(s: string): option[int] = if (s.trim.isempty) none else some(s.trim.length) }
and output...
scala> " hello" match { case state(l) => l } res25: int = 5
and things went south ...
scala> " " match { case state(l) => l } scala.matcherror: (of class java.lang.string) @ .<init>(<console>:9) @ .<clinit>(<console>) @ .<init>(<console>:7)
what bugging me, why did throw matcherror? clear explanation helpful.
the none
result means extractor input value unsuccessful, other extractors can considered, if no other specified, should throw matcherror
. documentation:
this equivalent
val name = customerid.unapply(customer2id).get
. if there no match,scala.matcherror
thrown:
val customerid(name2) = "--asdfasdfasdf"
Comments
Post a Comment