我有以下定义:
@Module
class WeaverDataModule {
// Provide the three pumps from providers
// All of them still explicitly mark 'Pump' as their return type
@Provides @IntoSet fun providesPump(thermosiphon: Thermosiphon) : Pump = thermosiphon
@Provides @IntoSet fun providesAnotherPump(suctionBased: SuctionBased) : Pump = suctionBased
@Provides @IntoSet fun providesGenericPump(genericPump: GenericPump) : Pump = genericPump
}
@Component(modules = [WeaverDataModule::class])
interface WeaverData {
// Get the CoffeeMaker
fun coffeeMaker(): CoffeeMaker
// Get the list of pumps
fun getPumps() : Set<Pump>
}
interface Pump
// The three pumps
class Thermosiphon @Inject constructor(val heater: Heater) : Pump
class SuctionBased @Inject constructor() : Pump
class GenericPump @Inject constructor() : Pump
// Some random heater
class Heater @Inject constructor()
Run Code Online (Sandbox Code Playgroud)
在我的代码中,当我执行以下操作时:
val cm = DaggerWeaverData.builder().build().getPumps()
Run Code Online (Sandbox Code Playgroud)
我确实按预期获得了三个泵。但是,当我尝试将其注入其他类时:
class CoffeeMaker @Inject constructor(
private val heater: Heater,
private val pump: Set<Pump>
) {
fun makeCoffee() =
"Making coffee with heater ${heater::class.java} and using pumps" +
" ${pump.map { it::class.java }.joinToString(",")}"
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
e: .../WeaverData.java:7: error: [Dagger/MissingBinding] java.util.Set<? extends weaver.Pump> cannot be provided without an @Provides-annotated method.
public abstract interface WeaverData {
^
java.util.Set<? extends weaver.Pump> is injected at
weaver.CoffeeMaker(…, pump)
weaver.CoffeeMaker is provided at
weaver.WeaverData.coffeeMaker()
Run Code Online (Sandbox Code Playgroud)
我也试过注射Collection<Pump>,但我仍然遇到类似的错误。在关于 multibinding的dagger 文档中,示例(在 Java 中)显示以下内容:
class Bar {
@Inject Bar(Set<String> strings) {
assert strings.contains("ABC");
assert strings.contains("DEF");
assert strings.contains("GHI");
}
}
Run Code Online (Sandbox Code Playgroud)
这正是我正在做的。对于基于构造函数的注入,它在 Kotlin 中工作得很好,因为以下编译和运行符合预期:
class CoffeeMaker @Inject constructor(
private val heater: Heater
) {
fun makeCoffee() =
"Making coffee with heater ${heater::class.java}"
}
Run Code Online (Sandbox Code Playgroud)
所以我对如何让这种多重绑定工作感到茫然。
Roh*_*bhu 13
所以事实证明你需要做的是:
class CoffeeMaker @Inject constructor(
private val heater: Heater,
private val pumps: Set<@JvmSuppressWildcards Pump>
) {
fun makeCoffee() =
"Making coffee with heater ${heater::class.java} with pumps ${pumps.map { it::class.java }.joinToString(",")}"
}
Run Code Online (Sandbox Code Playgroud)
这是因为Set在 Kotlin 中定义为 将Set<out E>其转换为 Java 为Set<? extends Pump>. 从类型理论的角度来看,Set<? extends Pump>与 不同Set<Pump>,因此 Dagger(可能)拒绝将其Set<Pump>视为可注入的Set<? extends Pump>,这是公平且正确的行为。
我们遇到的问题是,对于这些集合中的任何一个,由于默认情况下它们是不可变的,因此类型声明Set<X>将转换为Set<? extends X>,因为不可变集合仅在返回时具有对已解析类型的引用,因此是协变的。为了验证这个理论,以下也有效:
class CoffeeMaker @Inject constructor(
private val heater: Heater,
private val pumps: MutableSet<Pump>
) {
fun makeCoffee() =
"Making coffee with heater ${heater::class.java} with pumps ${pumps.map { it::class.java }.joinToString(",")}"
}
Run Code Online (Sandbox Code Playgroud)
注意 的使用MutableSet,它被定义为MutableSet<E> : Set<E> ...。这可能不是人们应该使用的东西,因为我怀疑这个集合实际上是可变的。所以我们需要的是让 kotlin 编译器将其Set<out E>视为Set<E>(在这种情况下可赋值是有效的,而不是相反)。所以这样做,我们使用@JvmSuppressWildcards注释。我希望这可以帮助其他面临类似问题的人。
| 归档时间: |
|
| 查看次数: |
1246 次 |
| 最近记录: |