All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.freya022.botcommands.internal.components.builder.BuilderInstanceHolderImpl.kt Maven / Gradle / Ivy

package io.github.freya022.botcommands.internal.components.builder

/**
 * Since the instances must be returned to provide a builder-like experience for JVM users,
 * we need to get the instance from the implementation itself
 *
 * However, overriding the [instance] property on the implementation does not allow delegates to access it,
 * as the delegates are in their own world.
 *
 * Kotlin does not support `this` in super constructors or in delegates,
 * even though this should be possible by just looking at the decompiled java code
 *
 * To work around this, we introduce [InstanceRetriever], which will hold our instance,
 * and will only be filled in the implementation's constructor, and so, the instance will be available post-construct.
 */
internal abstract class BuilderInstanceHolderImpl : BuilderInstanceHolder {
    protected abstract val instanceRetriever: InstanceRetriever

    override val instance: T
        get() = instanceRetriever.instance

    internal inline fun applyInstance(block: () -> Unit): T {
        block() // No need to give the instance
        return instance
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy