Dependency injection 有没有办法在kotlin kodein中创建一个工厂绑定,它可以生成null?
有没有办法创建一个工厂绑定,它可以生成null 比如说,Dependency injection 有没有办法在kotlin kodein中创建一个工厂绑定,它可以生成null?,dependency-injection,kotlin,kodein,Dependency Injection,Kotlin,Kodein,有没有办法创建一个工厂绑定,它可以生成null 比如说, bind<String?> with factory { x: Int -> when (x) { 1 -> "A" 2 -> "B" else -> null } } 与工厂绑定{x:Int-> 何时(x){ 1->“A” 2->“B” else->null } } 不幸的是,bind给出了错误。最后是可选的: bind<Op
bind<String?> with factory { x: Int ->
when (x) {
1 -> "A"
2 -> "B"
else -> null
}
}
与工厂绑定{x:Int->
何时(x){
1->“A”
2->“B”
else->null
}
}
不幸的是,
bind
给出了错误。最后是可选的:
bind<Optional<String>> with factory { x: Int ->
when (x) {
1 -> Optional.of<String>("A")
2 -> Optional.of<String>("B")
else -> Optional.empty()
}
}
与工厂绑定{x:Int->
何时(x){
1->可选。of(“A”)
2->可选。of(“B”)
else->Optional.empty()
}
}
这是正确答案。Kodein中的工厂不允许Null。