diff options
author | Adriaan Moors <adriaan@lightbend.com> | 2017-03-02 16:40:41 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-02 16:40:41 -0800 |
commit | 3f437a2aa448907ca608a1b68a9e71e5221c4632 (patch) | |
tree | ab2f9cec9a7b49e40f8fdaf8968619993762e6f6 /spec/05-classes-and-objects.md | |
parent | 011cc7ec86105640a6d606998f769986630fb62a (diff) | |
parent | 615849058b5452b9d54ac152a1380ca7f81998c9 (diff) | |
download | scala-3f437a2aa448907ca608a1b68a9e71e5221c4632.tar.gz scala-3f437a2aa448907ca608a1b68a9e71e5221c4632.tar.bz2 scala-3f437a2aa448907ca608a1b68a9e71e5221c4632.zip |
Merge pull request #5730 from adriaanm/userdefined-apply-211
Allow user-defined apply/unapply method in case class companion
Diffstat (limited to 'spec/05-classes-and-objects.md')
-rw-r--r-- | spec/05-classes-and-objects.md | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/spec/05-classes-and-objects.md b/spec/05-classes-and-objects.md index 69828ec7fe..65666e31cb 100644 --- a/spec/05-classes-and-objects.md +++ b/spec/05-classes-and-objects.md @@ -851,9 +851,8 @@ already a `val` or `var` modifier. Hence, an accessor definition for the parameter is [generated](#class-definitions). A case class definition of `$c$[$\mathit{tps}\,$]($\mathit{ps}_1\,$)$\ldots$($\mathit{ps}_n$)` with type -parameters $\mathit{tps}$ and value parameters $\mathit{ps}$ implicitly -generates an [extractor object](08-pattern-matching.html#extractor-patterns) which is -defined as follows: +parameters $\mathit{tps}$ and value parameters $\mathit{ps}$ implies +the definition of a companion object, which serves as an [extractor object](08-pattern-matching.html#extractor-patterns). It has the following shape: ```scala object $c$ { @@ -870,11 +869,13 @@ each $\mathit{xs}\_i$ denotes the parameter names of the parameter section $\mathit{ps}\_i$, and $\mathit{xs}\_{11}, \ldots , \mathit{xs}\_{1k}$ denote the names of all parameters in the first parameter section $\mathit{xs}\_1$. -If a type parameter section is missing in the -class, it is also missing in the `apply` and -`unapply` methods. -The definition of `apply` is omitted if class $c$ is -`abstract`. +If a type parameter section is missing in the class, it is also missing in the `apply` and `unapply` methods. + +If the companion object $c$ is already defined, +the `apply` and `unapply` methods are added to the existing object. +If the object $c$ already has a [matching](#definition-matching) +`apply` (or `unapply`) member, no new definition is added. +The definition of `apply` is omitted if class $c$ is `abstract`. If the case class definition contains an empty value parameter list, the `unapply` method returns a `Boolean` instead of an `Option` type and @@ -887,9 +888,6 @@ def unapply[$\mathit{tps}\,$]($x$: $c$[$\mathit{tps}\,$]) = x ne null The name of the `unapply` method is changed to `unapplySeq` if the first parameter section $\mathit{ps}_1$ of $c$ ends in a [repeated parameter](04-basic-declarations-and-definitions.html#repeated-parameters). -If a companion object $c$ exists already, no new object is created, -but the `apply` and `unapply` methods are added to the existing -object instead. A method named `copy` is implicitly added to every case class unless the class already has a member (directly defined or inherited) with that name, or the |