Methods and parameters

A method is declared with def, its parameters in parentheses and its result after a colon. The body is an expression, so the method returns the value of the last line and a return keyword is not part of the style: the compiler accepts one inside a method and the course does not write it.

A method and its result type

The result type of a public method is written out. It is the contract a caller reads, and it is what turns a mistake in the body into an error at the method rather than at the call site. The program below declares four methods and one entry point:

methods.scala

def add(a: Int, b: Int): Int = a + b def power(base: Int, exponent: Int = 2): Int = if exponent == 0 then 1 else base * power(base, exponent - 1) def describe(name: String, greeting: String = "Hello"): String = s"$greeting, $name" def total(numbers: Int*): Int = numbers.sum def format(value: Double)(pattern: String): String = pattern.format(value) @main def methods(): Unit = println(add(2, 3)) println(power(3)) println(power(3, 3)) println(describe("Ada")) println(describe(greeting = "Hi", name = "Grace")) println(total(1, 2, 3)) println(total()) println(format(3.14159)("%.2f"))

scala-cli run --server=false methods.scala

5
9
27
Hello, Ada
Hi, Grace
6
0
3.14

power is recursive and calls itself with a smaller exponent; the tail recursion lesson turns that shape into a loop. The body of add is on the same line as the signature because one expression needs no block.

Default and named arguments

A parameter with a default may be left out, and a named argument may be given in any order. The call describe(greeting = "Hi", name = "Grace") reads better than the positional form would, and it survives a later change to the parameter order. A named argument is also the way to fill the second parameter while taking the default for the first.

Scala forbids an overload that differs only in the defaults: two methods with the same name and the same parameter types are one method, and the compiler reports the duplicate. A default is evaluated at each call, so def now = System.currentTimeMillis() as a default would really read the clock every time.

Varargs

Int* takes any number of arguments, including none, and the parameter is a Seq[Int] inside the method. A collection is passed with : _*, which unpacks it into the argument list:

val values = List(4, 5, 6) println(total(values*))

That block is a fragment: it needs the total method above and a place inside a program, so the page shows the one line it teaches and not a whole file. The star form values* is the Scala 3 spelling; values: _* is the Scala 2 one and still works.

Several parameter lists

A method may have more than one parameter list, and the call fills them one pair of parentheses at a time. The form exists for a second list that is a configuration or a using clause, and it is what makes partial application possible: a call that stops after the first list returns a function of the rest, which the currying lesson uses.

Related

Автор статьи: Arthur Isaev

Изображение баннера