Tag: 仿制药

如何链接两个Kotlin函数参数的generics?

我想写一些像 fun check(thing: T, property: KProperty1, value: R) = property.get(thing) == value 以便 assertTrue(check(“Hello”, String::length, 5)) 但 assertTrue(check(“Hello”, String::length, “banana”)) 不编译。

如何以types安全的方式从通用列表中检索项目

我想把项目放在一个通用的容器中,但以types安全的方式检索它们。 容器在添加时会为每个项目添加一个序列号。 我试图在Kotlin中实现这个,但在pens()方法中遇到了问题。 有没有办法在函数参数中使用types信息来定义返回值的types? import java.util.concurrent.atomic.AtomicInteger import kotlin.reflect.KClass interface Item data class Pen(val name: String) : Item data class Eraser(val name: String) : Item data class Ref (val id: Int, val item: T) class Container { private val seq = AtomicInteger(0) private val items: MutableList<Ref> = mutableListOf() fun add(item: T) = items.add(Ref(seq.incrementAndGet(), item)) fun filter(cls: […]

Kotlin:方法不能和generics一起使用

作为这个的另一面: interface PacketDecoder : PacketTranscoder { fun decode(client: Client, buf: ByteBuf): T } 我正在尝试使用这个: interface PacketEncoder : PacketTranscoder { fun encode(packet: T, buf: ByteBuf) } 虽然PacketDecoder似乎工作正常,我不能引用encode(T, ByteBuf)方法。 IntelliJ IDEA自动完成甚至不显示它作为一个选项,手动写入它导致一个未解决的参考错误。 为了解决它,我目前正在使用这个黑客,但我不认为这是做到这一点的正确方法。 fun encode(packet: P, buf: ByteBuf)

什么是关键字在kotlin

我无法理解,我无法在kotlin中findout关键字的含义。 你可以在这里查看例子: List 如果有人能解释这个的意思。 这将是非常感激的。

kotlin列表上的filterNotNull具有genericstypes

这是工作: val values: List = listOf(“a”, null, “b”).filterNotNull() 这不工作: fun nonNullValues(values: List): List = values.filterNotNull() 编译器抱怨genericstypes: Error:(8, 63) Kotlin: Type parameter bound for T in fun kotlin.collections.Iterable.filterNotNull(): kotlin.collections.List is not satisfied: inferred type A is not a subtype of kotlin.Any 这一个工作: fun nonNullValues(values: List): List = values.filterNotNull() 有人可以请解释我为什么我需要告诉编译器,A是Any的子types? 我在想每个types都是Any的子types… 谢谢!

字段的安装程序通过types投影来删除

我有以下的SSCCE: class Foo(val bars: Map<Int, Bar>) { fun qux(baz: Baz) { val bar2 = bars[2]!! bar2.bazes += baz } interface Bar { var bazes: MutableList } } 这对我来说似乎很好,但编译器抱怨: Error:(5, 9) Kotlin: Setter for ‘bazes’ is removed by type projection 我不知道这意味着什么,更不用说如何纠正它。 这里发生了什么,如何解决这个问题?

Kotlin:返回一个通用的接口

在Kotlin,我试图编译以下内容: 给定一个与genericstypes的接口(Printer) 和该接口的两个实现(DogPrinter和CatPrinter) 根据外部variables(AnimalType)返回其中一台打印机 下面的代码不能编译,因为需要一个types: fun getMapper(animalType: AnimalType): Printer 我试图使用或但没有成功。 有人可以帮忙吗? (通过将以下代码复制到https://try.kotlinlang.org,很容易看出错误) enum class AnimalType { CAT, DOG } class Dog class Cat interface Printer { fun mapToString(input: T): String } class DogPrinter : Printer { override fun mapToString(input: Dog): String { return “dog” } } class CatPrinter : Printer { override fun mapToString(input: Cat): […]

在Kotlin中使用泛化generics的正确方法

我想重写SharedPreferences小扩展函数。 现在看起来像这样 fun SharedPreferences.put(arg: Pair) { val e = edit() val s = arg.second when (s) { is Int -> e.putInt(arg.first, s) is String -> e.putString(arg.first, s) is Boolean -> e.putBoolean(arg.first, s) is Long -> e.putLong(arg.first, s) else -> throw NotImplementedError(“Extension not implemented for this type”) } e.apply() } 是否有什么惯用的方式来使用generics? 我的第一个猜测是 inline fun SharedPreferences.put(arg: Pair) […]

generics类的types约束?

假设我有以下代码: sealed class Animal(val type: String) { data class Cat(val color: String) : Animal(“cat”) data class Dog(val mood: String , val ownersName : String) : Animal(“dog”) } abstract class AnimalDescriptor(val a: T) { abstract fun getSummary(): String } class CatDescriptor(a: Animal.Cat) : AnimalDescriptor(a) { override fun getSummary(): String { return “The color of this ${a.type} […]

为什么我不能在这个Rx变压器中使用接口作为通用types?

我在Kotlin中有以下数据类作为参考: interface GenericResponse { val error: Error? fun hasErrorObject(): Boolean = error != null } data class LoginResponse( val name: String, val auth: String, @SerializedName(“error”) override val error: Error? = null ) : GenericResponse LoginResponse实现GenericResponse 。 我正在使用以下方式的Retrofit API: @POST(“******”) fun createSession(@Body body: LoginBody): Single<Response> 此外,如果存在如下所示,我使用一个变换器从LoginResponse提取错误: class ExceptionTransformers { fun wrapRetrofitExceptionSingle(): (Single<Response>) -> Single { return […]