https://kotlinlang.org logo
Title
p

Pardip

07/13/2022, 1:46 PM
Is there way to avoid initialisation crash?
abstract class A {
	
	init {
		foo()
	}

	abstract fun foo()
}

class B : A {
	val b = /* init b */

	override fun foo() {
		b.something() // crash
	}
}
The super class call
foo()
on init so, the property
b
is not yet initialised
r

ribesg

07/13/2022, 2:22 PM
You could call
foo()
later one way or another, or implement
foo()
through composition instead of inheritance, or somehow pass the information you're trying to convey with
foo()
implementation through the super constructor, etc.
It's also not a Kotlin/Native issue
e
there should be a warning in IntelliJ when viewing
A
, something like "Calling non-final function foo in constructor"