From 9283b07d8aaca88cb8d592acaae11afeebab5cc9 Mon Sep 17 00:00:00 2001 From: Simon Ochsenreither Date: Fri, 12 Jun 2015 03:47:00 +0200 Subject: spec: Add 'Default Arguments' heading, sentence, example The sentence and the accompanying example were stolen from SID-1. --- spec/04-basic-declarations-and-definitions.md | 28 +++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/spec/04-basic-declarations-and-definitions.md b/spec/04-basic-declarations-and-definitions.md index 7fb5427d36..a8fd4cb60c 100644 --- a/spec/04-basic-declarations-and-definitions.md +++ b/spec/04-basic-declarations-and-definitions.md @@ -620,7 +620,11 @@ well as the function body, if it is present. A value parameter clause $\mathit{ps}$ consists of zero or more formal parameter bindings such as `$x$: $T$` or `$x: T = e$`, which bind value -parameters and associate them with their types. Each value parameter +parameters and associate them with their types. + +### Default Arguments + +Each value parameter declaration may optionally define a default argument. The default argument expression $e$ is type-checked with an expected type $T'$ obtained by replacing all occurences of the function's type parameters in $T$ by @@ -632,13 +636,7 @@ expression. Here, $n$ denotes the parameter's position in the method declaration. These methods are parametrized by the type parameter clause `[$\mathit{tps}\,$]` and all value parameter clauses `($\mathit{ps}_1$)$\ldots$($\mathit{ps}_{i-1}$)` preceding $p_{i,j}$. -The `$f\$$default$\$$n` methods are inaccessible for -user programs. - -The scope of a formal value parameter name $x$ comprises all subsequent -parameter clauses, as well as the method return type and the function body, if -they are given. Both type parameter names and value parameter names must -be pairwise distinct. +The `$f\$$default$\$$n` methods are inaccessible for user programs. ###### Example In the method @@ -657,6 +655,20 @@ def compare$\$$default$\$$1[T]: Int = 0 def compare$\$$default$\$$2[T](a: T): T = a ``` +The scope of a formal value parameter name $x$ comprises all subsequent +parameter clauses, as well as the method return type and the function body, if +they are given. Both type parameter names and value parameter names must +be pairwise distinct. + +A default value which depends on earlier parameters uses the actual arguments +if they are provided, not the default arguments. + +```scala +def f(a: Int = 0)(b: Int = a + 1) = b // OK +// def f(a: Int = 0, b: Int = a + 1) // "error: not found: value a" +f(10)() // returns 11 (not 1) +``` + ### By-Name Parameters ```ebnf -- cgit v1.2.3