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
}
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.
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).
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.
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With