matching - Why doesn't Scala optimize calls to the same Extractor? -


take following example, why extractor called multiple times opposed temporarily storing results of first call , matching against that. wouldn't reasonable assume results unapply not change given same string.

object name {   val namereg = """^(\w+)\s(?:(\w+)\s)?(\w+)$""".r    def unapply(fullname: string): option[(string, string, string)] = {     val namereg(fname, mname, lname) = fullname     some((fname, if (mname == null) "" else mname, lname))   } }  "john smith doe" match {   case name("jane", _, _) => println("i know you, jane.")   case name(f, "", _) => println(s"hi ${f}")   case name(f, m, _) => println(s"howdy, ${f} ${m}.")   case _ => println("don't know you") } 

wouldn't reasonable assume results unapply not change given same string.

unfortunately, assuming isn't enough (static) compiler. in order memoizing legal optimization, compiler has prove expression being memoized pure , referentially transparent. however, in general case, equivalent solving halting problem.

it possible write optimization pass tries prove purity of expressions , memoizes them iff , iff succeeds, may more trouble it's worth. such proofs hard quickly, succeed trivial expressions, execute anyway.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -