I haven't tried it but I imagine you should be abl...
# android
c
I haven't tried it but I imagine you should be able to. Higher order functions are first class citizens, after all. Have you tried anything yet?
@jc I've found an issue that had been opened and answered for you https://github.com/google/dagger/issues/900
j
Thank you @Can Orhan !
👍 1
r
I cant imagine a good use case for injecting a high order function . Why would you really want to
c
@rkeazor Perhaps an on click listener in Android that needs different behaviour based on build variant or something?
👍 2
r
hmm I see your point , i guess it could come in handy
did it end up working for you in the long run
by adding the surpressedWildcard anontation?
c
I personally haven't tried it. @jc?
👍 1
j
In our app we have MVP and Dagger 2. We have also a lot of RecyclerView’s adapters which need Listener’s for click actions. We try to avoid using Listeners which activities or fragments have to implement it. Right now we can inject presenter’s function directly to adapter. Inject must look something like this: @Module object MyModule { @Provides @JvmStatic @JvmSuppressWildcards @jsingle fun provideClickFunction(presenter: MyContract.Presenter): Function1<@JvmWildcard MyModel, Unit> = presenter::onModelClick @Provides @JvmStatic @jsingle fun providePresenter(presenter: MyPresenter): MyContract.Presenter = presenter @Provides @JvmStatic @jsingle fun provideAdapter(adapter: MyAdapter): ListAdapter<*, *> = adapter } class MyAdapter @Inject constructor(private val clickFunction: (MyModel) -> Unit) : ListAdapter ... { inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) { fun bind(model: MyModel) = itemView.setOnClickListener { clickFunction(model) } } }
I hope this dummy code is clear for you 🙂
r
What's wrong with the presenter not implementing the listener instead?
j
nothing it’s only a different approach 😉