diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2015-06-17 15:24:48 -0700 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2015-06-17 15:24:48 -0700 |
commit | 918ca9a0218868ae67c66264a8287824c23d46ff (patch) | |
tree | 225ca2990051ec90a0c4569cf71751b43d6313bc /spec | |
parent | 824e0b47bda251e90af842482baf3cf2bdbb080c (diff) | |
parent | 9283b07d8aaca88cb8d592acaae11afeebab5cc9 (diff) | |
download | scala-918ca9a0218868ae67c66264a8287824c23d46ff.tar.gz scala-918ca9a0218868ae67c66264a8287824c23d46ff.tar.bz2 scala-918ca9a0218868ae67c66264a8287824c23d46ff.zip |
Merge pull request #4551 from soc/topic/spec-default-args
spec: Add 'Default Arguments' heading, sentence, example
Diffstat (limited to 'spec')
-rw-r--r-- | spec/04-basic-declarations-and-definitions.md | 28 |
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 |