Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Scala know what method to invoke ( named parameters )

class Algo { 
    def a(  a : String = "Hola ",  b : String  = "adios" ) {
        print( a )
        print( b )
    }
    def a() { 
        print ("Uh?")
    }
}
object Algo { 
    def main( args : Array[String] ) { 
        new Algo().a()
    }
}

prints Uh?

If method a() is not defined, the code prints "Hola adios" using the default values.

So, I deduce, from this, that, if an exact signature is match, that is preffered.

Is this reasoning correct?

like image 234
OscarRyz Avatar asked Feb 11 '11 18:02

OscarRyz


People also ask

How do you pass parameters to a Scala function?

Scala - Functions with Named Arguments Named arguments allow you to pass arguments to a function in a different order. The syntax is simply that each argument is preceded by a parameter name and an equals sign. Try the following program, it is a simple example to show the functions with named arguments.

Is Scala call by reference or value?

Java and Scala both use call by value exclusively, except that the value is either a primitive or a pointer to an object. If your object contains mutable fields, then there is very little substantive difference between this and call by reference.

How do you pass parameters to a name?

When you pass an argument by name, you specify the argument's declared name followed by a colon and an equal sign ( := ), followed by the argument value.

What are by name parameters in Scala?

By-name parameters are evaluated every time they are used. They won't be evaluated at all if they are unused. This is similar to replacing the by-name parameters with the passed expressions. They are in contrast to by-value parameters. To make a parameter called by-name, simply prepend => to its type.


1 Answers

This behavior is clearly defined in SID #1, section 3.1.

Overloading Resolution In a method application expression, when multiple overloaded alternatives are applicable, the alternative which use default arguments is never selected.

like image 164
CheatEx Avatar answered Nov 11 '22 09:11

CheatEx