Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this reference 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 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.

like image 702
Luigi Plinge Avatar asked Sep 21 '11 11:09

Luigi Plinge


People also ask

What is an ambiguous reference?

An ambiguous reference is the situation in which a sentence contains a pronoun that could refer to either of two nouns in the same sentence or (using our new vocabulary word) where we have a pronoun but we aren't sure what its antecedent is.

What is ambiguous reference in C++?

Access to a base class member is ambiguous if you use a name or qualified name that does not refer to a unique function or object. The declaration of a member with an ambiguous name in a derived class is not an error. The ambiguity is only flagged as an error if you use the ambiguous member name.

How do you remove ambiguous reference in C#?

Solution 1 You can: 1) Remove the using statement for the one you are not interested in. 2) Fully qualify the object when you reference it, by prefixing "File" with either "Scripting." or "System.IO."


1 Answers

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.

like image 90
Jens Schauder Avatar answered Nov 15 '22 23:11

Jens Schauder