Is there any way to access the calling Class’ KCla...
# announcements
t
Is there any way to access the calling Class’ KClass object from an inline function? I think I’ve seen people abstract it from an empty lambda before, but I wanted to see if it was possible without this. It would be nice to not have to specific the exact type through a reified type parameter or
this::class
using a normal parameter.
c
In general, no, you’d have to do some kind of manual setup to cleanly get the calling class’s type, but using a reified type parameter of the inline function’s receiver makes it fairly transparent to the caller https://pl.kotl.in/FtzdLAaBZ. Apart from that, you’d basically have to step through the stackframes to determine the caller, but that only comes back as a String and is a bit of a hack. But I’ve got an example ehre of doing exactly that to infer a tag for a library I maintain https://github.com/copper-leaf/clog/blob/master/clog-core/src/jvmMain/kotlin/clog/impl/InferredTagFinder.kt
t
Ahh darn. The place I want to use it is already an extension function. But that’s very interesting to see the different implementations you’ve come up with. Thanks for sharing those snippets! It’s interesting because it seems like in the bytecode you would have access to the caller class, but I get that it probably takes a lot of work to integrate something like that into the language not to mention multiplatform.