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

commonMain.org.kodein.di.bindings.references.kt Maven / Gradle / Ivy

There is a newer version: 7.23.1
Show newest version
package org.kodein.di.bindings

/**
 * A reference gives a data and a function to later check the validity and retrieve that data.
 *
 *  @param current The value of the reference at the time of reference creation.
 *  @param next A function that returns the value of the reference, or null if the reference has become invalid, when later needed.
 */
public data class Reference(
    val current: T,
    val next: () -> T?
)

/**
 * A Function that creates a reference.
 */
public interface RefMaker {

    /**
     * A Function that creates a reference.
     *
     * @param T The type of the referenced object.
     * @param creator A function that can create a new T.
     * @return The referenced object and a function that returns the same object if the reference is still valid.
     */
    public fun  make(creator: () -> T): Reference
}

/**
 * A reference that acts as a Singleton: calls a creator function the first time, and then always return the same instance.
 */
public object SingletonReference : RefMaker {
    override fun  make(creator: () -> T): Reference {
        val value = creator()
        return Reference(value) { value }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy