Why is this link ambiguous?

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 type

val p: Point = peer.getMousePosition 

does it ok. Why?

edit: causes the problem:

class A {
  def value() = 123
}

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

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

does not cause a 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 should explain why this only happens when the methods are in different classes.

+5
source share
2 answers

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

Without annotation of type Scala does not know if you want a method reference in one parameter (object Function1) or if you want to call one who has no parameters (as a result Point).

Indication of the expected type clarifies your intentions.

Use getMousePosition()should work as well.

+9

- .

peer.getMousePosition()
+4

All Articles