Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - strange compile error (multiple overloaded alternatives of a method define default arguments)

Tags:

scala

In Scala IDE I get the following error about the class I am to compile:

in object MapArea, multiple overloaded alternatives of method addAnim define default arguments.

So yes, They do! So what? I do not understand the philosophy of this...

UPDATE:

It turns out that there could be a situation where compiler can't know which method to choose (as pointed out by Tomasz Nurkiewicz), ok I understand.. but in my situation these two methods can clearly be distinguished. Here is the exact piece of code (with all original names and stuff preserved this time):

  def addAnim (name: String, x: Float, y: Float, tex: Buffer[Texture], fps: Int, percent: Float = 0): TImageSequence =
    addAnim (name, x, y, tex(0).getImage.getWidth, tex(0).getImage.getHeight, tex, fps, percent)

  def addAnim (name: String, x: Float, y: Float, w: Float, h: Float, tex: Buffer[Texture], fps: Int, percent: Float = 0): TImageSequence = {
     // do stuff
  }
like image 975
noncom Avatar asked Jun 07 '12 07:06

noncom


People also ask

How to differentiate overloaded methods?

Overloaded methods are differentiated based on the number and type of the parameters passed as an argument to the methods. We can not define more than one method with the same name, Order and the type of the arguments. It would be a compiler error. The compiler does not consider the return type while differentiating the overloaded method.

Why does Pr1 have no parameters in Scala?

This is because pr1 has no parameters and Scala allows function calls without (). In the code I have attached it is treated as function call and you comment "Unit is not () => Unit". println ("...in pr1...") In the code above we basically do not make operator overloading (there is only one definition of output).

How to distinguish the methods with different method signatures in Scala?

Scala can distinguish the methods with different method signatures. i.e. the methods can have the same name but with different parameter list (i.e. the number of the parameters, the order of the parameters, and data types of the parameters) within the same class.


3 Answers

It isn't possible to have two methods with default parameters and with the same name. Scala generates methods to obtain default values with names based on target method's name, so some sort of name collision may occur.

scala> object Test {
     |   def m(i: String = "Default value") {}
     | }
defined module Test

scala> Test.`m$default$1`
res0: String = Default value
like image 88
senia Avatar answered Oct 19 '22 05:10

senia


You are not showing your code, but here is a simple example:

object C {
  def addAnim(x: Int = 42) {}
  def addAnim(y: String = "abc") {}
}

If I now call:

C.addAnim()

which method should be invoked? The object C won't compile because the compiler is not capable of guessing which addAnim method do you mean when not providing any argument.

like image 27
Tomasz Nurkiewicz Avatar answered Oct 19 '22 06:10

Tomasz Nurkiewicz


Why not combine both methods in one by making w and h optional as well, e.g.

def addAnim (name: String, 
    x: Float, y: Float, 
    tex: Buffer[Texture], 
    fps: Int, 
    percent: Float = 0, 
    w: Float = Float.NaN, h:Float = Float.NaN): TImageSequence
like image 40
Landei Avatar answered Oct 19 '22 05:10

Landei