Skip to content

Error "reference to <method> is ambiguous", importing methods with the same name from different scopes #3635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
scabug opened this issue Jul 2, 2010 · 2 comments

Comments

@scabug
Copy link

scabug commented Jul 2, 2010

I have the following code:

class Vector (val x: Double, val y: Double, val z: Double) {
  def *(f: Double) = new Vector(x * f, y * f, z * f)
  def +(v: Vector) = new Vector(x + v.x, y + v.y, z + v.z)
}

object Scope1 {
  def interpolate(t: Double, a: Double, b: Double): Double = a * (1.0 - t) + b * t
}

object Scope2 {
  def interpolate(t: Double, a: Vector, b: Vector): Vector = a * (1.0 - t) + b * t
}

class Example {
  import Scope1._
  import Scope2._

  def main(args: Array[String]) {
    val v = interpolate(0.3, 2.5, 4.5)
  }
}

When I compile this with Scala 2.8.0.RC6, I get an unexpected error message:

Example.scala:21: error: reference to interpolate is ambiguous;
it is imported twice in the same scope by
import Scope2._
and import Scope1._
val v = interpolate(0.3, 2.5, 4.5)
^
one error found

Why does the compiler think the reference is ambiguous? The two interpolate methods have different signatures and it should be clear which one I want to apply. Note that if I put them both in class Example, without importing them, it works without a problem:

// No problem
class Example {
  def interpolate(t: Double, a: Double, b: Double): Double = a * (1.0 - t) + b * t
  def interpolate(t: Double, a: Vector, b: Vector): Vector = a * (1.0 - t) + b * t

  def main(args: Array[String]) {
    val v = interpolate(0.3, 2.5, 4.5)
  }
}
@scabug
Copy link
Author

scabug commented Jul 2, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3635?orig=1
Reporter: Jesper (jesper)

@scabug
Copy link
Author

scabug commented Jul 2, 2010

@paulp said:
Duplicate of wontfix #2551.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant