summaryrefslogtreecommitdiff
path: root/spec/04-basic-declarations-and-definitions.md
diff options
context:
space:
mode:
authorSimon Ochsenreither <simon@ochsenreither.de>2015-06-12 03:47:00 +0200
committerSimon Ochsenreither <simon@ochsenreither.de>2015-06-12 03:47:00 +0200
commit9283b07d8aaca88cb8d592acaae11afeebab5cc9 (patch)
treeb42528cec6c4c80dd89133ba29602cc75f4554d3 /spec/04-basic-declarations-and-definitions.md
parent10a746551dce2ec61f52be74bbd3a476a3068568 (diff)
downloadscala-9283b07d8aaca88cb8d592acaae11afeebab5cc9.tar.gz
scala-9283b07d8aaca88cb8d592acaae11afeebab5cc9.tar.bz2
scala-9283b07d8aaca88cb8d592acaae11afeebab5cc9.zip
spec: Add 'Default Arguments' heading, sentence, example
The sentence and the accompanying example were stolen from SID-1.
Diffstat (limited to 'spec/04-basic-declarations-and-definitions.md')
-rw-r--r--spec/04-basic-declarations-and-definitions.md28
1 files 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