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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -