Here is a simple example that returns a lazily-initialized registrar from a related called link or standard property. I prefer to call from the called link because :: stands for reflection (logging related).
The class that Lazy<Logger> provides:
class LoggingProvider<T : Any>(val clazz: KClass<T>) { operator fun provideDelegate(inst: Any?, property: KProperty<*>) = lazy { LoggerFactory.getLogger(clazz.java) } }
Built-in functions for calling them:
inline fun <reified T : Any> KCallable<T>.logger() = LoggingProvider(T::class) inline fun <reified T : Any> T.logger() = LoggingProvider(T::class)
Here is an example of their use. The require statement in the initializer shows that the registrars have a link:
class Foo { val self: Foo = this val logger by this.logger() val callableLogger by this::self.logger() init { require(logger === callableLogger) } }
Preston garno
source share