I want to have `class A : B(), C(d)`, where `d` is...
# getting-started
m
I want to have
class A : B(), C(d)
, where
d
is a property inside
B
. I was assuming that the supertypes are instantiated in order, and so I should have access to
B.d
by the time I need it for the
C
constructor, but I am not sure how to express that. Is this possible?
e
you can only have a single superclass, how are you calling multiple superconstructors?
m
Oops, I simplified it too much for the example.
Copy code
class A(
 cProvider: () -> C
): B(), C by cProvider()
I want to modify the above code to add a property of
B
to be an argument of
cProvider()
. My understanding is that
B
already exists by the time
cProvider()
is called.
m
I don't think you can. You cannot reference this when using
by
. I assume you wouldn't be able to access super either.
s
Does an extra constructor help?
Copy code
class A private constructor(c: C) : B(c), C by c {
  constructor(cProvider: () -> C) : this(cProvider())
}
m
I wanted it the other way around:
Copy code
class A private constructor(c: C) : B(), C by c {
  constructor(cProvider: (D) -> C) : this(cProvider(d))
}
Where
d
is a property inside
B
. With the extra constructor I can indeed express that, as above, but I get an
Cannot access 'd' before the instance has been initialized
error, so it doesn't look doable.
s
I see, I misunderstood the problem 😞
m
The
by
is just syntax sugar for normal delegation, so the solution is to do what it does manually.
m
That indeed worked, thank you. The con is that now I have to manually delegate tens of properties / methods that the single
by
handled before.
e