gpt4 book ai didi

android - 具有绑定(bind)类型参数的通用父类(super class)型的 Kotlin typealias 不适用于继承

转载 作者:行者123 更新时间:2023-11-29 23:44:24 26 4
gpt4 key购买 nike

TL;DR 为什么这行得通:

interface SomeInterface
interface Generic <T : SomeInterface> {}
class Self : Generic<Self>, SomeInterface

这不是:

interface SomeInterface
interface Generic <T : SomeInterface> {}
typealias Specified = Generic<Self>
class Self : Specified, SomeInterface

Error: Type argument is not within its bounds: should be subtype of 'SomeInterface'

错误说它不是正确的子类型,但它是!

这种类型别名的用例来自现实生活中的代码,其中一个父类(super class)有 5 个类型参数,每个参数都有相当长的名称,我不想用不必要的垃圾邮件污染类头。有任何想法吗?我正在使用 kotlin 1.2.51。

---原始问题---

MVP 接口(interface):

interface MVPComponent // dagger component 
interface MVPComponentHolder<C : MVPComponent> // implementing class must provide component
interface MVPArgs // args passed from view to presenter on attach
interface MVPView<A : MVPArgs> // MVP View, must provide Args for presenter
interface MVPPresenter<T : MVPView<A>, A : MVPArgs, U : MVPUseCase>
interface MVPUseCase // selected API methods to use in presenter

MVP 的基本 fragment :

abstract class MVPFragment<F, V, P, A, C>
: Fragment(), MVPComponentHolder<C>
where F : V,
V : MVPView<A>,
P : MVPPresenter<V, A, *>,
C : MVPComponent<V>,
A : MVPArgs {
// lots of MVP logic
}

MVP 契约(Contract):

interface ExampleMVP {
data class Args(/* ... */) : MVPArgs

interface View : MVPView<Args> {
//...
}
interface Presenter : MVPPresenter<View, Args, UseCase> {
//...
}
interface UseCase : MVPUseCase<View> {
//...
}
}

最终 fragment :

class ExampleFragment : MVPFragment<
ExampleFragment,
ExampleMVP.View,
ExampleMVP.Presenter,
ExampleMVP.Args,
ExampleMVP>(), ExampleMVP.View {
// final fragment logic
}

但我想使用以下语法:

private typealias SuperFragment = MVPFragment<
ExampleFragment,
ExampleMVP.View,
ExampleMVP.Presenter,
ExampleMVP.Args,
ExampleMVP>

class ExampleFragment : SuperFragment(), ExampleMVP.View {
// final fragment logic
}

我将 ExampleFragment 作为 MVPFragment 的类型参数传递的原因是因为 Dagger 2 必须直接注入(inject)目标类(在这种情况下不仅仅是 FragmentMVPFragment,但 ExampleFragment),否则不会生成注入(inject)代码。

MVPFragment 中的注入(inject)如下所示:

@CallSuper
override fun onAttach(context: Context) {
super.onAttach(context)
component.injectIntoView(this as F) // must be target fragment type (here F)
}

最佳答案

问题是引入的循环依赖:

class Self : Specified, SomeInterface

如果 Self 没有继承自 Specified,它就可以工作。

更改后的示例如下所示。

interface SomeInterface
interface Generic <T : SomeInterface> {}
typealias Specified = Generic<Self>
class Self : SomeInterface

至于你原来的问题。我不认为你可以完全实现这一点,但必要的类型参数的数量可以像这样减少:

class ExampleFragment : SuperFragment<ExampleFragment>(), ExampleMVP.View {
// final fragment logic
}

private typealias SuperFragment<T> = MVPFragment<
T,
ExampleMVP.View,
ExampleMVP.Presenter,
ExampleMVP.Args,
ExampleMVP<ExampleMVP.View, ExampleMVP.Args>>

关于android - 具有绑定(bind)类型参数的通用父类(super class)型的 Kotlin typealias 不适用于继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51573584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com