Kotlin should be able to treat init as a regular function block which returns Unit.
This would be helpful for doing single call stuff like
init = println(“Hey there!”)
To aid with consistency, Kotlin could treat init as an actual function which is overridden from perhaps, Any. This is just an extra to this request because I’m not sure how it would pan out.
Maybe. I don’t really dislike this idea, but I don’t see a real usecase for this. Maybe for logging a debug statement? Not sure.
There are a few problems with this. The first is initialization of val
properties.
class Foo(a: Int) {
val a: Int
init {
this.a = a
}
}
In this case init
can’t be called as a regular function, because it would set a
for a second time.
The second problem is the function parameters. Are they the same as for the primary constructor?
class Foo(val a: Int) {
init = println("init Foo")
}
val f = Foo()
f.init()
// or
f.init(5)
You could say it depends on the parameters that are actually used inside the init block, but that would lead to situations where just logging a parameter would change the ABI of a class (which might be problematic). Also it would make implementing this much harder.
A last problem is the number of function this creates. While function count isn’t really a problem on PC it is a problem for android. Adding a function for each class is a significant amount and should be done only if there is a good reason.
TL;DR: Maybe for the new init syntax, probably not for the ability to call the init
block as a function.