Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package delegation
- import kotlin.properties.ReadOnlyProperty
- import kotlin.reflect.KProperty
- class Foo: Injectable by ScopeWrapper() {
- val bar: Bar by inject()
- }
- class Bar
- interface Injectable {
- var scope: Any?
- }
- class ScopeWrapper: Injectable {
- override var scope: Any? = null
- }
- fun Foo(scope: Any): Foo = Foo().apply { this.scope = scope }
- val foo = Foo("jfdksjief")
- //used for the `by` fields injection
- class InjectionDelegate<OWNER: Injectable, T>(val obj: OWNER, val clazz: Class<T>) : ReadOnlyProperty<OWNER, T> {
- override fun getValue(thisRef: OWNER, property: KProperty<*>): T {
- TODO()
- // obj.scope.getInstance(clazz)
- }
- }
- //provides a delegate per field
- //I actually wonder about the byte code cost in business classes
- //that would use a scope as a parameter, it seems to me the cost
- //of so many inline function overrides for reified types can be huge
- inline fun <reified T> Injectable.inject(): InjectionDelegate<Injectable, T> {
- return InjectionDelegate(this, T::class.java)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement