Skip to content

Commit

Permalink
Merge pull request #10620 from martijnhoekstra/spec-companion-apply
Browse files Browse the repository at this point in the history
draft: spec companion objects extending apply template
  • Loading branch information
som-snytt committed Dec 8, 2023
2 parents 198ed1b + c51e715 commit 233a548
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions spec/05-classes-and-objects.md
Expand Up @@ -862,11 +862,11 @@ 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}´ implies
parameters ´\mathit{tps}´ and value parameters ´\mathit{ps}´ with type ascriptions ´\mathit{pts}´ 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´ {
object ´c´ extends Function´\mathit{n}\,´[´\mathit{pt}_1\,\ldots\,\mathit{pt}_n´\,´c´[´\mathit{tps}\,´]]{
def apply[´\mathit{tps}\,´](´\mathit{ps}_1\,´)´\ldots´(´\mathit{ps}_n´): ´c´[´\mathit{tps}\,´] = new ´c´[´\mathit{Ts}\,´](´\mathit{xs}_1\,´)´\ldots´(´\mathit{xs}_n´)
def unapply[´\mathit{tps}\,´](´x´: ´c´[´\mathit{tps}\,´]) =
if (x eq null) scala.None
Expand All @@ -888,6 +888,8 @@ 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`.

It will not be modified to extend Function if the existing definition doesn't extend it.

If the case class definition contains an empty value parameter list, the
`unapply` method returns a `Boolean` instead of an `Option` type and
is defined as follows:
Expand Down

0 comments on commit 233a548

Please sign in to comment.