在Kodein中传递lambda作为参数
我正在使用Kodein在Android上进行依赖注入(当然是在Kotlin中),但是我正在努力的一个方面:我似乎无法将lambda作为参数传递给工厂。 它编译正确,但在运行时失败(我是通过Kodein来防范)。
在我的应用程序类,我做了以下绑定:
class MyApplication : Application(), KodeinAware { override val kodein by Kodein.lazy { ... bind<SimpleButtonListener>() with factory { func: () -> Unit -> SimpleButtonListener(func) } } }
在我的活动中,我这样调用它:
val onClick = { startActivity(EmailIntent()) } val clickListener : SimpleButtonListener by with(onClick).instance()
我也试过这个失败的:
val clickListener : SimpleButtonListener by with({ startActivity(EmailIntent()) }).instance()
但是我运行时总是遇到同样的问题:
com.github.salomonbrys.kodein.Kodein$NotFoundException: No factory found for bind<SimpleButtonListener>() with ? { ? } ... bind<SimpleButtonListener>() with factory { Function0<out Unit> -> SimpleButtonListener } ...
对于Kotlin,我还是个新手,所以我不确定我究竟在哪里出了问题。 在我所错过的语言中是否有一个古怪或习惯用法,或者是否有作为参数的kodein限制在lamdbas周围?
这是Kodein 4中的一个错误,在下一个版本的Kodein(版本5.0)中得到纠正。
与此同时,这里是修复:
val clickListener : SimpleButtonListener by With(generic(), onClick).instance()
对不起,不便之处