I've got a logging helper function like this: ```i...
# announcements
b
I've got a logging helper function like this:
Copy code
inline fun <reified T : Any> T.createChildLogger(
    parentLogger: Logger,
    childContext: Map<String, String> = emptyMap()
): Logger =
    parentLogger.createChildLogger(getClassForLogging(T::class.java).name, childContext)
Which is used to abstract out the logic for obtaining the 'name' of the logger using the caller's class, but I'm finding I can't invoke it in-place when calling a parent constructor, like so:
Copy code
class Foo(parentLogger: Logger) : Parent(createChildLogger(parentLogger))
I assume this is because the receiver function can't be applied here? Is there a workaround for something like this? (either in the definition of
createChildLogger
, or how it would be invoked by the child class?
Originally I'd actually hoped I could just define
createChildLogger
as an inline function (not a receiver) and obtain the calling class that way, but I don't know if there's any good way to dynamically 'detect' if it's running from within a class or not
So the receiver works well to scope it to only be called within objects, but it'd be a bummer if it didn't resolve correctly in this case
Ah, for some reason I thought you could pass
this
to a parent (meaning it was at least somewhat accessible, and therefore could get the class), but I just tested that out and it seemed like it isn't accessible at all:
Copy code
interface Handler

open class Parent(val handler: Handler)

class Child : Handler, Parent(this)
complains about
"this" is not defined in this context
I can use the
operator invoke
trick on the companion object to work around it, but wondering if there's a better way