Compatibility with Java hierarchies that demand multiple constructors, such as
- Android Views
- Swing dialogs
With a primary constructor:
class Foo(a: Bar): MySuper() {
// when there's a primary constructor, (direct or indirect) delegation to it is required
constructor() : this(Bar()) { ... } // can't call super() here
constructor(s: String) : this() { ... }
}
No primary constructor:
class Foo: MySuper { // initialization of superclass is not allowed
constructor(a: Int) : super(a + 1) { ... } // must call super() here
}
No primary constructor + two overloaded constructors
class Foo: MySuper { // initialization of superclass is not allowed
constructor(a: Int) : super(a + 1) { ... }
constructor() : this(1) { ... } // either super() or delegate to another constructor
}
- is delegation allowed when no primary constructor is present?
- Allow omitting parameterless delegating calls?
- There's a primary constructor if
- parentheses after class name, or
- there're no secondary constructors (default primary constructor)
- No parentheses after name and an explicit constructor present => no primary constructor
No primary constructor => no supertype initialization allowed in the class header:
class Foo : Bar() { // Error
constructor(x: Int) : this() {}
}
When a primary constructor is present, explicit constructors are called secondary.
Every class must have a constructor. The following is an error:
class Parent
class Child : Parent { }
The error is: "superclass must be initialized". This class has a primary constructor, but does not initialize its superclass in the class header.
constructor
: modifiers "constructor" valueParameters (":" constructorDelegationCall) block
;
constructorDelegationCall
: "this" valueArguments
| "super" valueArguments
;
Passing lambdas outside parentheses is not allowed in constructorDelegationCall
.
The only situation when an explicit constructor may not have an explicit delegating call is
- when there's no primary constructor and the superclass has a constructor that can be called with no parameters passed to it
class Parent {}
class Child: Parent {
constructor() { ... } // implicitly calls `super()`
}
If there's a primary constructor, all explicit constructors must have explicit delegating calls that (directly or indirectly) call the primary constructor.
class Parent {}
class Child(): Parent() {
constructor(a: Int) : this() { ... }
}
The primary constructor's body consists of
- super class initialization from class header
- assignments to properties from constructor parameters declared with
val
orvar
- property initializers and bodies of anonymous initializers following in the order of appearance in the class body
If the primary constructor is not present, property initializers and anonymous initializers are conceptually "prepended" to the body of each explicit constructor that has a delegating call to super class, and their contents are checked accordingly for definite initialization of properties etc.
Anonymous initializer in the class body must be prefixed with the init
keyword, without parentheses:
class C {
init {
... // anonymous initializer
}
}
All constructors must be checked for
- absence of circular delegation
- overload compatibility
- definite initialization of all properties that must be initialized
- absence of non-empty super call for enum constructors
No secondary constructors can be declared for
- traits
- objects (named, anonymous, and default)
- bodies of enum literals
Data classes should have a primary constructor