How to introduce Kotlin into the transferred property using dagger 2?

I need to inject ( @Named) with dagger2 into the delegated kotlin property.

//works great!
@set:Inject var cat: Cat by Ref(ref)

//fails
@set:[Inject Named("dog")] var dog : Animal by Ref(ref)

So i tried

//fails, can't use `@field` with a delegated property
@field:[Inject Named("dog")] var dog : Animal by Ref(ref)

//fails, can't use `lateinit` with a delegated property
@field:[Inject Named("dog")] lateinit var dog : Animal by Ref(ref)
+4
source share
1 answer

You cannot, because such a property does not have a field, but instead, delegates receive and set delegation calls to the property object. And the dagger does not know anything about this deletet, these are just 2 methods and an object.

I'm not sure about your case, but maybe you can insert your Ref delegate class instance with the required value, but I donโ€™t know what you want to do, how your delegate works and how you use the Dagger (what type of injection you use)

0
source

Source: https://habr.com/ru/post/1670693/


All Articles