Files
kotlin-fork/idea/resources/inspectionDescriptions/LeakingThis.html
T

31 lines
789 B
HTML

<html>
<body>
This inspection reports dangerous operations inside constructors including:
<ul>
<li>Accessing a non-final property in constructor</li>
<li>Calling a non-final function in constructor</li>
<li>Using <b>this</b> as a function argument in a constructor of a non-final class</li>
</ul>
These operations are dangerous because your class can be inherited,
and a derived class is not yet initialized at this moment. Typical example:
<br /><br />
<pre>
<b>abstract class</b> Base {
<b>val</b> code = calculate()
<b>abstract fun</b> calculate(): Int
}
<b>class</b> Derived(<b>private val</b> x: Int) : Base() {
<b>override fun</b> calculate() = x
}
<b>fun</b> testIt() {
println(Derived(42).code) <i>// Expected: 42, actual: 0</i>
}
</pre>
</body>
</html>