Creator Applications
Creator applications allow to use simple function call syntax to create instances of a class, even if there is no apply method implemented. Example:
class StringBuilder(s: String) {
def this() = this("")
}
StringBuilder("abc") // same as new StringBuilder("abc")
StringBuilder() // same as new StringBuilder()
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:
Given a function call f(args)
,
- if
f
is a method applicable toargs
, typecheckf(args)
unchanged, - otherwise, if
f
has anapply
method applicable toargs
as a member, continue withf.apply(args)
, - otherwise, if
f
is of the formp.m
and there is an implicit conversionc
applicable top
so thatc(p).m
is applicable toargs
, continue withc(p).m(args)
There's now a fourth rule following these rules:
- otherwise, if
f
is syntactically a stable identifier, andnew f
wheref
is interpreted as a type identifier is applicable toargs
, continue withnew f(args)
.
Analogously, the possible interpretations of a function call with type arguments f[targs]
are augmented with the following interpretation as a final fallback:
- if
f
is syntactically a stable identifier, andnew f[targs]
wheref
is interpreted as a type identifier is well-typed, continue withnew f[targs]
.
Motivation
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).
Discussion
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
caused numerous problems, including
- overloading ambiguities
- overriding errors
- shadowing of user-defined
apply
methods by more specific auto-generated ones.