You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/reference/other-new-features/creator-applications.md
+20-9Lines changed: 20 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -3,43 +3,54 @@ layout: doc-page
3
3
title: "Universal Apply Methods"
4
4
---
5
5
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`.
6
+
Scala case classes generate apply methods, so that values of case classes can be created using simple
7
+
function application, without needing to write `new`.
7
8
8
9
Scala 3 generalizes this scheme to all concrete classes. Example:
10
+
9
11
```scala
10
12
classStringBuilder(s: String):
11
13
defthis() =this("")
12
14
13
15
StringBuilder("abc") // same as new StringBuilder("abc")
14
16
StringBuilder() // same as new StringBuilder()
15
17
```
16
-
This works since a companion object with two apply methods
18
+
19
+
This works since a companion object with two `apply` methods
17
20
is generated together with the class. The object looks like this:
The synthetic object `StringBuilder` and its `apply` methods are called _constructor proxies_.
24
29
Constructor proxies are generated even for Java classes and classes coming from Scala 2.
25
30
The precise rules are as follows:
26
31
27
-
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.
32
+
1. A constructor proxy companion object `object C` is created for a concrete class `C`,
33
+
provided the class does not have already a companion, and there is also no other value
34
+
or method named `C` defined or inherited in the scope where `C` is defined.
28
35
29
36
2. Constructor proxy `apply` methods are generated for a concrete class provided
30
37
31
-
- the class has a companion object (which might have been generated in step 1), and
32
-
- that companion object does not already define a member named `apply`.
38
+
- the class has a companion object (which might have been generated in step 1), and
39
+
- that companion object does not already define a member named `apply`.
33
40
34
-
Each generated `apply` method forwards to one constructor of the class. It has the
35
-
same type and value parameters as the constructor.
41
+
Each generated `apply` method forwards to one constructor of the class. It has the
42
+
same type and value parameters as the constructor.
36
43
37
-
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).
44
+
Constructor proxy companions cannot be used as values by themselves. A proxy companion object must
45
+
be selected with `apply` (or be applied to arguments, in which case the `apply` is implicitly
46
+
inserted).
38
47
39
48
Constructor proxies are also not allowed to shadow normal definitions. That is,
40
49
if an identifier resolves to a constructor proxy, and the same identifier is also
41
50
defined or imported in some other scope, an ambiguity is reported.
42
51
43
52
### Motivation
44
53
45
-
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).
54
+
Leaving out `new` hides an implementation detail and makes code more pleasant to read. Even though
55
+
it requires a new rule, it will likely increase the perceived regularity of the language, since case
56
+
classes already provide function call creation syntax (and are often defined for this reason alone).
0 commit comments