【问题标题】:Issue with generics for extension function to Reactor Mono in KotlinKotlin 中 Reactor Mono 扩展函数的泛型问题
【发布时间】:2020-05-07 12:31:43
【问题描述】:

我正在尝试创建一个DTO 接口,该接口具有将 dto 转换回其原始模型的方法。现在我想为Mono 类创建一个扩展函数,以提高可读性。但我无法让它工作,谁能帮帮我。

class StringDTO(private val s: String) : DTO<String> {
    override fun fromModel(model: String): DTO<String> {
        TODO("Not yet implemented")
    }

    override fun toModel(): String {
        return s
    }
}

val s = Mono.just("asd")
    .map { StringDTO(it) }
    .toModel<String>()
    .block()

println("s = ${s}")

fun <T> Mono<DTO<T>>.toModel(): Mono<T> {
    return this.map(DTO<T>::toModel)
}

【问题讨论】:

    标签: generics kotlin project-reactor


    【解决方案1】:

    在扩展函数中用out修饰符声明Mono的类型参数解决了这个问题:

    fun <T> Mono<out DTO<T>>.toModel(): Mono<T> {
        return this.map(DTO<T>::toModel)
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-20
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多