Repository navigation
Flexible constructor bodies #511
Replies: 2 comments 3 replies
Do I understand right, that this is allowed: abstract class Thing(val name: String)
class Hammer : Thing {
val state = computeState()
constructor(size: String) : super("$size hammer")
constructor(sizeInCm: Int) : super("$sizeInCm cm hammer")
}but here it is ambiguous where initializer takes place? abstract class Thing(val name: String)
class Hammer : Thing {
val state = computeState()
constructor(size: String) {
super("$size hammer")
}
constructor(sizeInCm: Int) : super("$sizeInCm cm hammer")
}I agree that it is not entirely obvious where the initializer runs, but the model "initializers and init blocks run after the prologue" seems to be consistent.
What about |
|
Reading the current KEEP-0472, I think your reading of the two examples is correct as a description of the proposed rules:
I agree that "run initializers after successful delegation, before the constructor epilogue" sounds like a possible alternative semantics. The KEEP's restriction seems deliberately conservative, but it does not spell out why that ordering could not be specified for mixed header/body delegation (and for a On the second question: |
Uh oh!
There was an error while loading. Please reload this page.
This is the discussion related to the flexible constructor bodies proposal. The current full text of the proposal can be found here.
This proposal introduces new places in which a constructor may call its
superconstructor or anotherthisconstructor, apart from the header. This unlocks the possibility of executing some validation logic or pre-processing of the arguments before the instance construction takes place. This proposal aligns with JEP 513 in the JVM platform.All reactions