Skip to content

Commit a8a45d0

Browse files
committed
Adjust the format of creator-applications.md
1 parent 4a59cd2 commit a8a45d0

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,54 @@ layout: doc-page
33
title: "Universal Apply Methods"
44
---
55

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`.
78

89
Scala 3 generalizes this scheme to all concrete classes. Example:
10+
911
```scala
1012
class StringBuilder(s: String):
1113
def this() = this("")
1214

1315
StringBuilder("abc") // same as new StringBuilder("abc")
1416
StringBuilder() // same as new StringBuilder()
1517
```
16-
This works since a companion object with two apply methods
18+
19+
This works since a companion object with two `apply` methods
1720
is generated together with the class. The object looks like this:
21+
1822
```scala
1923
object StringBuilder:
2024
inline def apply(s: String): StringBuilder = new StringBuilder(s)
2125
inline def apply(): StringBuilder = new StringBuilder()
2226
```
27+
2328
The synthetic object `StringBuilder` and its `apply` methods are called _constructor proxies_.
2429
Constructor proxies are generated even for Java classes and classes coming from Scala 2.
2530
The precise rules are as follows:
2631

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.
2835

2936
2. Constructor proxy `apply` methods are generated for a concrete class provided
3037

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`.
3340

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.
3643

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).
3847

3948
Constructor proxies are also not allowed to shadow normal definitions. That is,
4049
if an identifier resolves to a constructor proxy, and the same identifier is also
4150
defined or imported in some other scope, an ambiguity is reported.
4251

4352
### Motivation
4453

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

Comments
 (0)