Function as a method

Most Scala developers came to it from Java. Because of this, probably the most common way is to define a method inside of a class, trait, or an object, like in the following familiar example:

class MethodDefinition {
def eq(arg1: String, arg2: Int): Boolean = ! nonEqual(arg1, arg2)
private def nonEq(a: String, b: Int) = a != b.toString
}

By convention, we've explicitly defined a return type for the public method in the same way that we would do for the return type in Java. For the non recursive function, the result type can be omitted. We've done this for the private method. 

The type declaration for the value parameters is mandatory.

Each value parameter can have one default value assigned to it:

def defaultValues(a: String = "default")(b: Int = 0, c: String = a)(implicit d: Long = b, e: String = a) = ???

The preceding snippet also demonstrates that it is possible to define multiple groups of value parameters. The parameters from the last group can be implicit and also can be provided with default values. The default values from consecutive groups can refer to the parameters defined in previous groups as e refers to the default value of c, which is a.

The type of the value parameter prefixed with => means that this parameter should not be evaluated at the moment the method is called, but instead each time the parameter is referenced in the body of the method. Such arguments are called by-name parameters and basically, they represent a zero-argument method with the argument's return type:

scala> def byName(int: => Int) = {
| println(int)
| println(int)
| }
byName: (int: => Int)Unit
scala> byName({ println("Calculating"); 10 * 2 })
Calculating
20
Calculating
20

In this example, we can see how the passed block of code is executed twice, matching the number of usages inside the method's body.

The * (star) can be added as a suffix to the name of the type of the last value parameter to denote that this is a repeated parameter and it takes a number of arguments of the defined type. The given arguments are then available in the method body as a collection.Seq of the specified type:

def variable(a: String, b: Int*): Unit = {
val bs: collection.Seq[Int] = b
}

variable("vararg", 1, 2, 3)
variable("Seq", Seq(1, 2, 3): _*)

It is illegal to pass the Seq direct in place of repeated parameters. The last line in the previous snippet shows the :_* syntax to mark the last parameter as a sequence argument. The repeated parameter can't take default values.

Arguments in the method definition have names. These names can be used to call the method by providing arguments in any order (as opposed to the order specified in the definition of the method):

def named(first: Int, second: String, third: Boolean) = s"$first, $second, $third"

named(third = false, first = 10, second = "Nice")
named(10, third = true, second = "Cool")

The named and normal arguments can be mixed, as shown in the last line of the previous code. In this case, the positional arguments must be specified first.

Until now, we defined our examples in the scope of the enclosing class or object. But Scala gives more flexibility in this regard. A method can be defined in any valid scope. This makes it local to the enclosing block and thus limits its visibility. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.80.100