Scala 3 — Book

Methods

Language

Scala methods

Scala classes, case classes, traits, enums, and objects can all contain methods. The syntax of a simple method looks like this:

def methodName(param1: Type1, param2: Type2): ReturnType =
  // the method body
  // goes here

Here are a few examples:

def sum(a: Int, b: Int): Int = a + b
def concatenate(s1: String, s2: String): String = s1 + s2

You don’t have to declare a method’s return type, so you can write those methods like this, if you prefer:

def sum(a: Int, b: Int) = a + b
def concatenate(s1: String, s2: String) = s1 + s2

This is how you call those methods:

val x = sum(1, 2)
val y = concatenate("foo", "bar")

Here’s an example of a multiline method:

def getStackTraceAsString(t: Throwable): String = {
  val sw = new StringWriter
  t.printStackTrace(new PrintWriter(sw))
  sw.toString
}
def getStackTraceAsString(t: Throwable): String =
  val sw = new StringWriter
  t.printStackTrace(new PrintWriter(sw))
  sw.toString

Method parameters can also have default values. In this example, the timeout parameter has a default value of 5000:

def makeConnection(url: String, timeout: Int = 5000): Unit =
  println(s"url=$url, timeout=$timeout")

Because a default timeout value is supplied in the method declaration, the method can be called in these two ways:

makeConnection("https://localhost")         // url=http://localhost, timeout=5000
makeConnection("https://localhost", 2500)   // url=http://localhost, timeout=2500

Scala also supports the use of named parameters when calling a method, so you can also call that method like this, if you prefer:

makeConnection(
  url = "https://localhost",
  timeout = 2500
)

Named parameters are particularly useful when multiple method parameters have the same type. At a glance, with this method you may wonder which parameters are set to true or false:

engage(true, true, true, false)

The extension keyword declares that you’re about to define one or more extension methods on the parameter that’s put in parentheses. As shown with this example, the parameter s of type String can then be used in the body of your extension methods.

This next example shows how to add a makeInt method to the String class. Here, makeInt takes a parameter named radix. The code doesn’t account for possible string-to-integer conversion errors, but skipping that detail, the examples show how it works:

extension (s: String)
  def makeInt(radix: Int): Int = Integer.parseInt(s, radix)

"1".makeInt(2)      // Int = 1
"10".makeInt(2)     // Int = 2
"100".makeInt(2)    // Int = 4

See also

Scala Methods can be much more powerful: they can take type parameters and context parameters. They are covered in detail in the Domain Modeling section.

Contributors to this page: