stephanenicolas / toothpick

A scope tree based Dependency Injection (DI) library for Java / Kotlin / Android.
Apache License 2.0
1.12k stars 115 forks source link

Add some KTP extensions #404

Open osipxd opened 4 years ago

osipxd commented 4 years ago

I've created some extensions for my project. Maybe it should be in KTP?

/** Shorter call of `bind(...).toInstance(...)`. Class of binding retrieved from [T]. */
inline fun <reified T : Any> Module.bindInstance(instance: T) {
    bind(T::class).toInstance(instance)
}

/** Simplified syntax to install one module to scope. */
inline fun Scope.installModule(crossinline bindings: Module.() -> Unit): Scope {
    return installModules(module { bindings() })
}

/** Close scope. */
fun Scope.close() {
    KTP.closeScope(name)
}
dlemures commented 4 years ago

Thank you for the idea!

Regarding the first one: Module.bindInstance we try to keep the following pattern so the api is predictable: bind(x).to...

We will consider the other two for TP 4 as we plan to improve the API.

Thx again :)