I've found that I can only call a receiver functio...
# announcements
b
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
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.
Why does that
double
function even have a receiver? It's not used...
b
Interesting
It's a simplified example
Thanks for explaining!