How to call a function defined inside a companion ...
# announcements
k
How to call a function defined inside a companion object, if I can only get to the companion object by reflection, so:
SomeClass::class.companionObjectInstance
y
Cast it to the type
SomeClass.Companion
or to an interface that you know it'll implement. Worst case scenario, just reflect over the functions on the
companionObjectInstance
k
Thanks for the info, making the Companion object implement an interface and then casting to it worked great!
t
Reject the dark side and seek light (solution that doesn't require reflection) : ) Maybe if tell us why you can only obtain companion object via reflection, I can figure something out.
☝️ 1
k
Is reflection really seen as that bad? 🙂 I'm kind of new to Kotlin, I'm porting an app that uses macros a lot, reflection seems like a natural fit for that (or compiler plugins, but that's above my paygrade for now)
t
@Karlo Lozovina Reflaction is slow, not-typesafe and it works on static definitions that cannot be generated on runtime without code manipulation.
k
I figured as much, so far I'm doing all the reflection work at startup away from any hot loops and such, as far as typesafety goes I haven't been bitten that badly yet 😆
t
Yes, but the more I wait for some spring-boot servers to start, the more I am convinced that it's time to focus on doing whatever can be done during build during build. This also creates division between server and client technologies, which is bad for ecosystem.
1