Skip to content
Search

Scala:NamedArguments

lastmod 2020-09-11created 2020-09-111 edits

When calling methods and functions, you can explicitly use the names of the variables in the call, as follows.

Example

def printName(first:String, last:String) = {
  println(first + " " + last)
}
printName("John","Smith")  // "John Smith"를 출력
printName(first = "John",last = "Smith")  // "John Smith"를 출력
printName(last = "Smith",first = "John")  // "John Smith"를 출력

Once parameter names are used in a call, their order does not matter as long as every parameter is named. This feature works well together with Default Parameter Values.

def printName(first:String = "John", last:String = "Smith") = {
  println(first + " " + last)
}
printName(last = "Jones")  // "John Jones"를 출력

Since you can place parameters in whatever order you like, you can use default values starting from the most important parameter in the parameter list.

See also

Favorite site

author Zer0 sitemap