I've found that I can only call a receiver function while invoking a parent class' constructor if a companion object is defined. Is this some subtle type system thing? Or a bug?
Copy code
fun <T : Any> T.double(num: Int): Int = num * 2
abstract class Parent(val num: Int)
// Works
class Child(
num: Int
) : Parent(double(num)) {
private companion object
}
// Error: unresolved reference 'double'
class Child2(num: Int) : Parent(double(num))
d
diesieben07
01/30/2020, 11:05 PM
In your "Works" example the receiver is inferred to be the companion object, which is the only possible receiver in scope.
In the 2nd case there is no such receiver (
this
) cannot be leaked to outside functions here, because the object is not yet fully constructed.