Tag: 泛型

在Kotlin中创建通用的二维数组

假设我有一个泛型类,我需要一个泛型类型T的二维数组。 如果我尝试以下 class Matrix<T>(width: Int, height: Int) { val data: Array<Array<T>> = Array(width, arrayOfNulls<T>(height)) } 编译器会抛出一个错误,说“ 不能使用'T'作为实体类型参数,而是使用一个类。 ”

通过子类来改变Kotlin中类型参数的变化

这个问题来源于我之前关于Kotlin泛型的问题 。 请在这里查看更多关于这个问题背后动机的信息。 我有一个类不受约束的类型参数 trait Handler<T> { fun handle(result: T) } 我需要创建一个Handler实例,其中T是List<O> ,因此是不可变的。 我的想法是子类Handler和注释它作为一个消费者(即通过使用) – trait ListHandler<in T>: Handler<List<T>> { } 然而,这给了我一个错误,说: “参数T被声明为'在',但发生在'不变'的位置Handler<List<T> ” 错误是什么意思,有什么办法可以解决它?

Kotlin:我怎样才能调用一个lambda字段,它有一个泛型类?

我怎样才能调用一个lambda字段有一个泛型类的类? 出于某种原因,像Example<*>这样的泛型类的引用会产生一个用Nothing代替原始类型(如Example<Something> )的接受。 我怎样才能调用这样一个lambda通过仅参考Example<*> ? Codebase我遇到这个问题: https : //github.com/Jire/Acelta/tree/master/src/main/kotlin/com/acelta/packet/incoming/packets 我正在尝试这个代码,但正如你可以看到在第五行我不能调用lambda,因为接受类型是Nothing 。 private val incoming = arrayOfNulls<Packet<*>>(256) fun incoming(id: Int, packeteer: Packeteer) { val packet = incoming[id] ?: return packet.receive(packeteer, /* this is type Nothing! */) }

Kotlin泛型方法和继承

在我的代码中,我想在抽象类中创建一个方法,该方法返回一些Observable。 然后这个抽象类的实现将返回某些(指定)类型的Observable。 不幸的是,Android Studio会在实现方法()中返回一个错误“类型不匹配”: 预计:可观察 找到:Observable <{package} .DrawerItemEntity> 我的MockDrawerList.getList()返回Observable<DrawerItemEntity> 请关注execute()和buildUseCaseObservable 抽象类 public abstract class UseCase(threadExecutor: ThreadExecutor, postExecutionThread: PostExecutionThread) { private val threadExecutor: ThreadExecutor private val postExecutionThread: PostExecutionThread private var subscription: Subscription = Subscriptions.empty() init { this.postExecutionThread = postExecutionThread this.threadExecutor = threadExecutor } protected abstract fun buildUseCaseObservable(): Observable<Any> public fun execute(useCaseSubsriber: Subscriber<Any>) { subscription = buildUseCaseObservable() […]

可变的地图在Kotlin

我在我的项目中使用NamedParameterJdbcTemplate并传递参数: MapSqlParameterSource(mapOf( "userId" to userId, "count" to count )) 我不想一直写第一行,我想创建自己的函数,它将采用字符串对的值对: params( "userId" to userId, "count" to count ) 但是当我尝试实现它时,我遇到了泛型的问题(我不在这里发布错误描述): fun params(vararg pairs: Pair<String, Any>) = MapSqlParameterSource(mapOf(pairs)) 你能请正确的实施建议吗?

Kotlin泛型期待意想不到的类型

我用两个泛型定义了下面的基类,并使用它深两层(缺少一个更好的短语)。 这是我的用例。 abstract class BasePresenter<out M, out V> { var model: M? = null var view: WeakReference<V>? = null fun setM(model: M?): Unit { this.model = model if (setupDone()) { updateView() } } fun bindView(view: V) { this.view = WeakReference(view) } fun unbindView() { this.view = null } abstract fun updateView() fun view(): V? { […]

区分Kotlin中模棱两可的扩展

当我提到rng.max时,我遇到了编译器“不能在以下候选者中选择”的问题: rng:IntRange : inline val <T:Comparable<T>> ClosedRange<T>.max get() = endInclusive // max of range inline val <T:Comparable<T>> Iterable<T>.max get():T? = max() // max element 我认为这是因为一个ClosedRange实现ClosedRange和(间接通过IntProgression ) Iterable 。 如果我想编写一个适用于CharRange , LongRange和CharRange (可能但不一定是扩展ClosedRange )的扩展ClosedRange ,是否有任何可以明确解析为ClosedRange语法,而不是相似名称的Iterable在这种情况下扩展?

Kotlin覆盖子类型的乐趣

我无法继承包含一个基类型的方法/乐趣的接口,我想重写作为实现它的类中的子类型。 到目前为止,我有界面 interface IModel { fun convert(dataModel: BaseDataModel) } 而实施它的班级: class SettingsModel: IModel { override fun convert(dataModel: BaseDataModel) { // Conversion of models here } } 而且我也有SettingsDataModel这是: class SettingsDataModel: BaseDataModel() { } 我想要实现的是每个类/模型实现IModel,能够得到具体的DataModel,如: class SettingsModel: IModel { override fun convert(dataModel: SettingsDataModel) { // Conversion of models here } } 而不需要施放它。 我想我不能,因为它修改了有趣的签名,使其不是一个真正的重写。 我尝试使用泛型和通用约束,但没有运气: interface IModel { […]

Kotlin泛型的问题

传递给onResume时发生错误。 不知何故,它不承认this实现ActivityLifecycleType ,我错过了什么? open class BaseActivity<ViewModelType: ActivityViewModel<*>>: RxAppCompatActivity(), ActivityLifecycleType { protected var viewModel: ViewModelType? = null @CallSuper override fun onResume() { super.onResume() viewModel?.onResume(this) ==> Error Required Nothing, Find BaseActivity<ViewModelType> } } open class ActivityViewModel<in ViewType: ActivityLifecycleType> { fun onResume(view: ViewType) { // Do something } } interface ActivityLifecycleType { fun lifecycle(): Observable<ActivityEvent> }

Kotlin属性:“属性的类型参数必须在其接收器类型中使用”

我有以下简单的Kotlin扩展功能: // Get the views of ViewGroup inline val ViewGroup.views: List<View> get() = (0..childCount – 1).map { getChildAt(it) } // Get the views of ViewGroup of given type inline fun <reified T : View> ViewGroup.getViewsOfType() : List<T> { return this.views.filterIsInstance<T>() } 此代码编译和工作正常。 但是,我希望函数getViewsOfType成为一个属性,就像views 。 Android Studio甚至建议。 我让AS做重构,它生成这个代码: inline val <reified T : View> ViewGroup.viewsOfType: List<T> […]