This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun interface SamFoo { | |
fun bar() : String | |
} | |
val samFoo = SamFoo { | |
"hello world" | |
} | |
samFoo.bar() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface RegularFoo { | |
fun bar() : String | |
} | |
val regularFoo = object : RegularFoo { | |
override fun bar(): String { | |
return "hello world" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun module(scope: SimpleDiScope.() -> Unit) { | |
scope.invoke(SimpleDiScope) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object SimpleDiScope { | |
inline fun <reified T : Any> factory(factory: InstanceType.Factory<T>) { | |
SimpleDiStorage.addFactory(factory) | |
} | |
inline fun <reified T : Any> factoryWithParams(factory: InstanceType.ParamFactory<T>) { | |
SimpleDiStorage.addFactory(factory) | |
} | |
inline fun <reified T : Any> singleton(factory: InstanceType.Factory<T>) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T : Any> get(noinline params: (Params.() -> Unit)? = null): T { | |
return SimpleDiStorage.getInstance(params) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PublishedApi | |
internal object SimpleDiStorage { | |
val instances = mutableMapOf<KClass<*>, InstanceType<*>>() | |
inline fun <reified T : Any> addFactory(factory: InstanceType<T>) { | |
check(instances[T::class] == null) { | |
"Definition for ${T::class} already added." | |
} | |
instances[T::class] = factory | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val aInstance = get<A>() // or val aInstance: A = get() | |
val bInstance = get<B>() | |
val cInstance = get<C> { | |
params(bInstance) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface A { | |
fun foo() | |
} | |
class AImpl: A { | |
override fun foo() { | |
print("A") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module { | |
singleton<A> { AImpl() } | |
factory<B> { | |
BImpl( | |
a = get() | |
) | |
} | |
factoryWithParams<C> { (aParam) -> | |
C( | |
a = aParam as A, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed interface InstanceType<T> { | |
fun interface Factory<T> : InstanceType<T> { | |
fun build(): T | |
} | |
fun interface ParamFactory<T> : InstanceType<T> { | |
fun build(vararg params: Any): T | |
class Params { | |
var parameters: Array<out Any> = arrayOf() |
NewerOlder