gpt4 book ai didi

dependency-injection - 有没有办法在 kotlin kodein 中创建一个可以产生 null 的工厂绑定(bind)?

转载 作者:行者123 更新时间:2023-12-02 13:23:33 29 4
gpt4 key购买 nike

有没有办法创建一个可以产生 null 的工厂绑定(bind)?

例如,

bind<String?> with factory { x: Int -> 
when (x) {
1 -> "A"
2 -> "B"
else -> null
}
}

不幸的是, bind<String?>给出错误。

最佳答案

Optional 结束:

bind<Optional<String>> with factory { x: Int -> 
when (x) {
1 -> Optional.of<String>("A")
2 -> Optional.of<String>("B")
else -> Optional.empty()
}
}

关于dependency-injection - 有没有办法在 kotlin kodein 中创建一个可以产生 null 的工厂绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48996945/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com