Skip to content

Commit 19651ff

Browse files
committed
Update doc page
1 parent ef38e0c commit 19651ff

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

docs/docs/reference/other-new-features/creator-applications.md

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
---
22
layout: doc-page
3-
title: "Creator Applications"
3+
title: "Universal Apply Methods"
44
---
55

6-
Creator applications allow using simple function call syntax to create instances
7-
of a class, even if there is no apply method implemented. Example:
6+
Scala case classes generate apply methods, so that values of case classes can be created using simple function application, without needing to write `new`.
7+
8+
Scala 3 generalizes this scheme to all concrete classes. Example:
89
```scala
910
class StringBuilder(s: String) {
1011
def this() = this("")
@@ -13,31 +14,33 @@ class StringBuilder(s: String) {
1314
StringBuilder("abc") // same as new StringBuilder("abc")
1415
StringBuilder() // same as new StringBuilder()
1516
```
16-
Creator applications generalize a functionality provided so far only for case classes, but the mechanism how this is achieved is different. Instead of generating an apply method, the compiler adds a new possible interpretation to a function call `f(args)`. The previous rules are:
17-
18-
Given a function call `f(args)`,
17+
This works since a companion object with two apply methods
18+
is generated together with the class. The object looks like this:
19+
```scala
20+
object StringBuilder {
21+
inline def apply(s: String): StringBuilder = new StringBuilder(s)
22+
inline def apply(): StringBuilder = new StringBuilder()
23+
}
24+
```
25+
The synthetic object `StringBuilder` and its `apply` methods are called _constructor proxies_.
26+
Constructor proxies are generated even for Java classes and classes coming from Scala 2.
27+
The precise rules are as follows:
1928

20-
- if `f` is a method applicable to `args`, typecheck `f(args)` unchanged,
21-
- otherwise, if `f` has an `apply` method applicable to `args` as a member, continue with `f.apply(args)`,
22-
- otherwise, if `f` is of the form `p.m` and there is an implicit conversion `c` applicable to `p` so that `c(p).m` is applicable to `args`, continue with `c(p).m(args)`
29+
1. A constructor proxy companion object `object C` is created for a concrete class `C`, provided the class does not have already a companion, and there is also no other value or method named `C` defined or inherited in the scope where `C` is defined.
2330

24-
There's now a fourth rule following these rules:
31+
2. Constructor proxy `apply` methods are generated for a concrete class provided
2532

26-
- otherwise, if `f` is syntactically a stable identifier, and `new f` where `f` is interpreted as a type identifier is applicable to `args`, continue with `new f(args)`.
33+
- the class has a companion object (which might have been generated in step 1), and
34+
- that companion object does not already define a member named `apply`.
2735

28-
Analogously, the possible interpretations of a function call with type arguments `f[targs]` are augmented with the following interpretation as a final fallback:
36+
Each generated `apply` method forwards to one constructor of the class. It has the
37+
same type and value parameters as the constructor.
2938

30-
- if `f` is syntactically a stable identifier, and `new f[targs]` where `f` is interpreted as a type identifier is well-typed, continue with `new f[targs]`.
39+
Constructor proxy companions cannot be used as values by themselves. A proxy companion object must be selected with `apply` (or be applied to arguments, in which case the `apply` is implicitly inserted).
3140

41+
Constructor proxies are also not allowed to shadow normal definitions. That is,
42+
if an identifier resolves to a constructor proxy, and the same identifier is also
43+
defined or imported in some other scope, an ambiguity is reported.
3244
### Motivation
3345

3446
Leaving out `new` hides an implementation detail and makes code more pleasant to read. Even though it requires a new rule, it will likely increase the perceived regularity of the language, since case classes already provide function call creation syntax (and are often defined for this reason alone).
35-
36-
### Discussion
37-
38-
An alternative design would auto-generate `apply` methods for normal classes, in the same way it is done now for case classes. This design was tried but abandoned since it
39-
caused numerous problems, including
40-
41-
- overloading ambiguities
42-
- overriding errors
43-
- shadowing of user-defined `apply` methods by more specific auto-generated ones.

docs/sidebar.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ sidebar:
9393
url: docs/reference/other-new-features/trait-parameters.html
9494
- title: Transparent Traits
9595
url: docs/reference/other-new-features/transparent-traits.html
96-
- title: Creator Applications
96+
- title: Universal Applies
9797
url: docs/reference/other-new-features/creator-applications.html
9898
- title: Export Clauses
9999
url: docs/reference/other-new-features/export.html

0 commit comments

Comments
 (0)