我在 Kodein 模块中有以下代码
bind<Manager>() with factory { strategy: OrderStrategyType ->
val manager: Manager = when (strategy) {
OrderStrategyType.VOLATILITY -> VolatilityManager()
else -> SimpleManager()
}
return@factory manager
}
Manager
接口在哪里,VolatilityManager()
并且SimpleManager()
正在实现它。
IntelliJ 建议内联变量manager
,如果我应用建议,我会收到代码:
bind<Manager>() with factory { strategy: OrderStrategyType ->
return@factory when (strategy) {
OrderStrategyType.VOLATILITY -> VolatilityManager()
else -> SimpleManager()
}
}
但是,虽然 IDE 检测到此代码没有问题,但它无法编译
Type inference failed: infix fun <C, A> with(binding: KodeinBinding<in C, in A, out Manager>): Unit
cannot be applied to
(Factory<Any?, OrderStrategyType, Any>)
老实说,我不明白编译器错误。推理对我来说很明显。我应该重写我的代码,如果是,如何?