From 66f52fe143fe781af618b66e14236d6e7a6375f7 Mon Sep 17 00:00:00 2001 From: Travis Lee Date: Thu, 9 Mar 2017 15:46:57 +0100 Subject: [PATCH] Rewrote named arguments tour --- .../tour/_posts/2017-02-13-named-arguments.md | 35 ++++++++++++++++ .../_posts/2017-02-13-named-parameters.md | 41 ------------------- 2 files changed, 35 insertions(+), 41 deletions(-) create mode 100644 tutorials/tour/_posts/2017-02-13-named-arguments.md delete mode 100644 tutorials/tour/_posts/2017-02-13-named-parameters.md diff --git a/tutorials/tour/_posts/2017-02-13-named-arguments.md b/tutorials/tour/_posts/2017-02-13-named-arguments.md new file mode 100644 index 0000000000..4b6eba84f1 --- /dev/null +++ b/tutorials/tour/_posts/2017-02-13-named-arguments.md @@ -0,0 +1,35 @@ +--- +layout: tutorial +title: Named Arguments + +disqus: true + +tutorial: scala-tour +categories: tour +num: 34 +previous-page: default-parameter-values +prerequisite-knowledge: function-syntax +--- + +When calling methods, you can label the arguments with their parameter names like so: + +```tut + def printName(first: String, last: String): Unit = { + println(first + " " + last) + } + + printName("John", "Smith") // Prints "John Smith" + printName(first = "John", last = "Smith") // Prints "John Smith" + printName(last = "Smith", first = "John") // Prints "John Smith" +``` +Notice how the order of named arguments can be rearranged. However, if some arguments are named and others are not, the unnamed arguments must come first and in the order of their parameters in the method signature. + +``` +def printName(first: String, last: String): Unit = { + println(first + " " + last) +} + +printName(last = "Smith", "john") // Does not compile +``` + +Note that named arguments do not work with calls to Java methods. diff --git a/tutorials/tour/_posts/2017-02-13-named-parameters.md b/tutorials/tour/_posts/2017-02-13-named-parameters.md deleted file mode 100644 index 058d75599f..0000000000 --- a/tutorials/tour/_posts/2017-02-13-named-parameters.md +++ /dev/null @@ -1,41 +0,0 @@ ---- -layout: tutorial -title: Named Parameters - -disqus: true - -tutorial: scala-tour -categories: tour -num: 34 -previous-page: default-parameter-values ---- - -When calling methods and functions, you can use the name of the variables explicitly in the call, like so: - -```tut - def printName(first:String, last:String) = { - println(first + " " + last) - } - - printName("John","Smith") - // Prints "John Smith" - printName(first = "John",last = "Smith") - // Prints "John Smith" - printName(last = "Smith",first = "John") - // Prints "John Smith" -``` - -Note that once you are using parameter names in your calls, the order doesn't matter, so long as all parameters are named. This -feature works well with [default parameter values]({{ site.baseurl }}/tutorials/tour/default-parameter-values.html): - -```tut - def printName(first:String = "John", last:String = "Smith") = { - println(first + " " + last) - } - - printName(last = "Jones") - // Prints "John Jones" -``` - -Since you can place the parameters in any order you like, you can use the default value for parameters that come first in the -parameter list.