Skip to content

Commit c18ba91

Browse files
committed
Add docs
1 parent 6d8cf3f commit c18ba91

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

docs/docs/reference/features-classification.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ These features replace existing constructs with the aim of making the language s
4444
of value classes while guaranteeing absence of boxing,
4545
- [Toplevel definitions](https://dotty.epfl.ch/docs/reference/dropped-features/package-objects.html) replace package objects, dropping syntactic boilerplate,
4646
- [Vararg patterns](https://dotty.epfl.ch/docs/reference/changed-features/vararg-patterns.html) now use the form `: _*` instead of `@ _*`, mirroring vararg expressions,
47-
- [Synthesized creation methods](https://contributors.scala-lang.org/t/expunging-new-from-scala-3/2868/81) allow to use simple function call syntax
48-
instead of `new` expressions (under discussion, not implemented).
47+
- [Creator applications](https://dotty.epfl.ch/docs/reference/other-new-features/creator-applications.html) allow to use simple function call syntax
48+
instead of `new` expressions. `new` expressions stay around as a fallback for
49+
the cases where creator applications cannot be used.
4950

5051
With the exception of early initializers and old-style vararg patterns, all superseded features continue to be available in Scala 3.0. The plan is to deprecate and phase them out later.
5152

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
layout: doc-page
3+
title: "Creator Applications"
4+
---
5+
6+
Creator applications allow to use simple function call syntax to create instances
7+
of a class, even if there is no apply method implemented. Example:
8+
```scala
9+
class StringBuilder(s: String) {
10+
def this() = this(s)
11+
}
12+
13+
StringBuilder("abc") // same as new StringBuilder("abc")
14+
StringBuilder() // same as new StringBuilder()
15+
```
16+
Creator applications generalize a functionality provided so far only for case classes, but the mechanism how this is achieved is different. Instead 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)`,
19+
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)`
23+
24+
There's now a fourth rule following these rules:
25+
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)`.
27+
28+
Analogously, the possible interpretations of a function call with type arguments `f[targs]` are augmented with the following interpretation as a final fallback:
29+
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]`.
31+
32+
### Motivation
33+
34+
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ sidebar:
6969
subsection:
7070
- title: Trait Parameters
7171
url: docs/reference/other-new-features/trait-parameters.html
72+
- title: Creator Applications
73+
url: docs/reference/other-new-features/creator-applications.html
7274
- title: Inlining by Rewriting
7375
url: docs/reference/other-new-features/inline.html
7476
- title: Meta Programming

0 commit comments

Comments
 (0)