7
import swing._

object PeerTest extends SimpleSwingApplication {
  def top = new MainFrame {
    val p = peer.getMousePosition 
  }
}

gives

error: ambiguous reference to overloaded definition,
both method getMousePosition in class Container of type (x$1: Boolean)java.awt.Point
and  method getMousePosition in class Component of type ()java.awt.Point
match expected type ?
val p = peer.getMousePosition

but adding the type

val p: Point = peer.getMousePosition 

makes it ok. Why?

edit: causes problem:

class A {
  def value() = 123
}

class B extends A {
  def value(b: Boolean) = 42  
}

object Main extends App {
  println ((new B).value) 
}

doesn't cause problem:

class A {
  def value() = 123
  def value(b: Boolean) = 42  
}

class B extends A {}

object Main extends App {
  println ((new B).value) 
}

So I think the answer has to explain why it only occurs when the methods are in different classes.

Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180

2 Answers2

10

There are two methods getMousePosition one without and one with a boolean parameter.

Without a type annotation Scala does not know if you want a reference to the method in one parameter (a Function1 object) or if you want to invoke the one without parameters (resulting in a Point).

Specifying the expected type clarifies your intend.

Using getMousePosition() should work as well.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • 1
    So we can break people's code if we decide to add overloading methods to a class... this doesn't seem very good. I thought putting a `_` after the method was the way to make it a partially applied function. – Luigi Plinge Sep 21 '11 at 12:08
  • 1
    @Luigi There are many ways to break people's code by adding overloads, this is just one more way. – Daniel C. Sobral Sep 21 '11 at 14:11
  • @Daniel @retronym There might be reasons to avoid overloading but I don't think this case is ambiguous, since it wouldn't be interpreted as a function if the zero-arg overload didn't exist (unless a `_` is added). It looks like a bug to me, unless there's some reason I don't know of why it should behave this way. – Luigi Plinge Sep 21 '11 at 16:54
4

A more direct way to refer to the desired overloaded alternative is by including the empty argument list.

peer.getMousePosition()
retronym
  • 54,768
  • 12
  • 155
  • 168