有没有办法做到以下几点?
class Someotherthing
class SomeotherthingDTO
class Something
class SomethingDTO
fun convert(entity: Someotherthing): SomeotherthingDTO = SomeotherthingDTO()
fun convert(entity: Something): SomethingDTO = SomethingDTO()
fun <T, D> generic(entity: T): D {
// TODO: check here if there is convert() that accepts type T?! somehow? reflection? reification? or it will be possible only in the future by using typeclasses (KEEP-87)?
return convert(entity)
}
fun main() {
val x: SomethingDTO = convert(Something())
println(x.toString())
}目前,结果是:以下代码都不能用提供的参数调用...
发布于 2020-11-09 16:52:56
你需要多个接收器才能工作(KEEP-87)。有了这些,你将能够正确地“找到”接收器。
在此之前,我通常要做的是将Converters放在ConverterRegistry中进行如下转换:
interface Converter<A, B> {
val fromClass: KClass<A>
val toClass: KClass<B>
fun convert(from: A): B
fun convertBack(from: B): A
}
interface ConverterRegistry {
fun <A, B> tryConvert(from: KClass<A>, to: KClass<B>): B?
}https://stackoverflow.com/questions/64719513
复制相似问题