Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Given this 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, but byname fails to compile:

ambiguous reference to overloaded definition

Why? I would expect to observe the same behavior for by-value and by-name parameters with respect to overloading… How can it be fixed?

like image 878
Jean-Philippe Pellet Avatar asked Dec 01 '10 14:12

Jean-Philippe Pellet


1 Answers

That's because JVM does not support a "by-name" parameter, so Scala has to implement it in another way. => X actually compiles to a Function0[X], which erases to Function0[Object], which makes it impossible for Scala to distinguish two methods that differ only by the expected type of a by-name parameter.

like image 130
Daniel C. Sobral Avatar answered Nov 15 '22 19:11

Daniel C. Sobral