Why is Scala's behavior in case of overloading with by-name parameters different from the case with by-value parameters? -


given scala code:

object test {    def byval(a: int) = println("int")   def byval(a: long) = println("long")    def byname(a: => int) = println("=> int")   def byname(a: => long) = println("=> long")    def main(args: array[string]) {       byval(5)       byname(5)   } } 

the call byval(5) compiles correctly, byname fails compile:

ambiguous reference overloaded definition 

why? expect observe same behavior by-value , by-name parameters respect overloading… how can fixed?

that's because jvm not support "by-name" parameter, scala has implement in way. => x compiles function0[x], erases function0[object], makes impossible scala distinguish 2 methods differ expected type of by-name parameter.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -