Tag: 仿制药

Kotlin:方法不能和泛型一起使用

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

筛选泛型类型而不反映或投射

在Kotlin有一个有限的形式化的泛型 。 有没有什么办法可以使用实体过滤泛型类型,而不使用getClass()或as或任何一种怪异的注释,即。 只是通过使用is关键字? 例如,我有以下结构: import java.util.* internal class Layout<out T : LayoutProtocol>(val t: T) { fun getName(): String { return t.getName() } } interface LayoutProtocol { fun getName(): String } internal class Vertical : LayoutProtocol { override fun getName(): String { return "Vertical" } } internal class Horizontal : LayoutProtocol { override fun getName(): […]

我们可以在Kotlin中使用infix通用方法吗?

编译器接受中缀+泛型方法,但是使用它的语法是什么? 例如,给定那些2个相同的方法(模随机泛型): infix inline fun Int1.plus1(i: Int1) = Int1(this.value + i.value) infix inline fun <U> Int1.plus2(i: Int1) = Int1(this.value + i.value) 我可以写: Int1(3).plus1(Int1(4)) Int1(3) plus1 Int1(4) Int1(3).plus2<Int>(Int1(4)) 但是这个电话是无效的: Int1(3) plus2<Int> Int1(4) 有人可以解释我为什么?

在方法中使用泛型类型的接口

我对Kotlin的泛型感到困惑。 如何在函数参数中使用T类的类(在addNewItem()和deleteItem())? 我得到错误“类型参数T被声明为'出',但发生在'在'位置kotlin” interface IStorageManager<out T: IFileItem> { fun getAllItems(): List<T> fun addNewItem(itemToAdd: T) fun deleteItem(itemToDelete: T) } 屏幕

请参阅Kotlin中的Generic类型

具有以下课程结构: abstract class GenericClass<T: Fragment> { protected lateinit var fragment: T fun anOperation(){ //do something } } class ConcreteA: GenericClass<AFragment> { } class ConcreteB: GenericClass<BFragment>{ } 我怎样才能做到这一点? fun useFragment(f:GenericClass){ f.anOperation() } 当我声明useFragment函数时,我得到“ One type argument expected for class GenericClass ”

Kotlin中泛型类的扩展函数

下面我的扩展功能有什么问题 class Foo<T> { fun <T> Foo<T>.plus(that: Foo<T>): Foo<T> = throw Exception() init { Foo<Int>() + Foo<String>() // A receiver of type Foo<T> is required } } 更新 我想知道为什么它不同于普通的扩展函数,T成功地推断为Any并希望实现相同的行为,例如T被推断为Foo <Any> class Foo { fun <T> T.foo(that: T): T = throw Exception() init { "str" foo 42 } }

Kotlin KClass实例里面的扩展功能

我需要访问通用扩展函数中的类类型。 例如,如果我想创建一个扩展函数来返回我正在扩展的类型成员的集合,我会这样做: fun <T> T.getMembers() = this::class.memberProperties 问题是我不能做这个:: class和T ::类里面的函数。 有没有办法访问泛型类的类类型?

在Kotlin中混合类和函数泛型?

我试图创建一个接口,定义一个单一的方法。 该方法的参数类型必须是接口的类型参数,因为接口的实现将具有唯一的此参数实现。 到目前为止,罚款是,该参数也有一个类型的参数是绑定到该方法。 我可以有一个类型参数定义为一个类类型参数,它也有自己的类型参数绑定到该方法? 换句话说: 我试图建立一个API实现两个客户端接口。 每个客户端根据传递到它们的rxecute函数的Request<T>对象(RxJava +执行,是的,我过分地为这个名字感到骄傲)进行api调用。 我的问题是如何在界面中定义rxecute方法。 让我列出一些示例代码(我的实际代码是不使用impl ,不用担心)。 interface Request<T> // T is return type of the request class RequestImpl<T>: Request<T> { // impl } interface ApiClient<R : Request<*>> { // R should somehow be of type Request<T> fun <T> rxecute(request: R): Single<T> } class ApiClientImpl : ApiClient<RequestImpl<????>> { override fun <T> […]

Kotlin转换为泛型类

如何将下面的代码转换为泛型类,以便可以指定类型参数T而不是具体类型User ? class UserService : BaseService { val query = datastore.createQuery(User::class.java) }

Kotlin泛型在类层次结构中不匹配

为什么这不工作? 它在Java中工作。 class MyList : java.util.LinkedList<String>() fun main(args: Array<String>) { val x: java.util.List<String> = MyList() } 我明白了 Type mismatch: inferred type is MyList but List<String> was expected 对于分配线。 链接在线评估: http : //try.kotlinlang.org/#/UserProjects/70dhmnocn8ueh73hg0o61mp01f/8ormftvrpbimfu0l3uf37galv