如何实现一个继承自父抽象类的带有泛型类型的接口

huangapple go评论56阅读模式
英文:

How to implement an interface with a generic type inherited from parent abstract class

问题

抱歉,代码部分不要翻译,以下是翻译好的内容:

"Unfortunately the above does not work, and I believe it is because despite the interface being inside an abstract class does not necessarily mean it is an instance of it, but its own file which happens to be located inside that class."

"不幸的是,上述方法不起作用,我认为原因是尽管接口位于抽象类内部,但这并不一定意味着它是抽象类的一个实例,而是它自己的文件,恰好位于该类内部。"

"I came up with a way to achieve what I want - but I would really prefer to use an interface for this since it would be cleaner and easier to update. This is what I have that does what I want:"

"我想出了一种实现我想要的方式 - 但我真的更喜欢使用接口,因为这样会更清晰,更容易更新。以下是我想要的实现方式:"

"But again, I would prefer a lot more if I could achieve the same via an interface without having to specify the type. How can I accomplish that?"

"但再次强调,如果我能够通过接口实现相同的功能而无需指定类型,那将更加理想。我该如何实现这一点?"

英文:

I have a situation in which I am attempting to inherit the generic type of an abstract class to its internal interface.

For example:

MyAbstractClass.kt

abstract class MyAbstractClass<T> {

    fun interface MyInterface {
        suspend fun doSomething(param: T)
    }

    private var _callback: MyInterface? = null

    fun someFunction(callback: MyInterface?) {
        (...)
    }

    (...)

}

Unfortunately the above does not work, and I believe it is because despite the interface being inside an abstract class does not necessarily mean it is an instance of it, but its own file which happens to be located inside that class.

I understand I could just do the following:

MyInterface.kt

fun interface MyInterface<T> {
    suspend fun doSomething(param: T)
}

And problem would be solved, but that would not work for my purpose because then it would have to be used like this:

// the type is only implicit in the lambda because it was made explicit in the function
myAbstractClassInstance.someFunction<ExplicitTypeHere> { it: ExplicitTypeHere -> 
    (...)
}

Whereas I want to have the type inherited so functions like the above can be written like this:

// the type is implicit in the lambda now without having to specify it beforehand
myAbstractClassInstance.someFunction { it: ExplicitTypeHere -> 
    (...)
}

I came up with a way to achieve what I want - but I would really prefer to use an interface for this since it would be cleaner and easier to update. This is what I have that does what I want:

MyAbstractClass.kt

abstract class MyAbstractClass<T> {

    private var _callback: (suspend (param: T) -> Unit)? = null

    fun someFunction(callback: (suspend (param: T) -> Unit)?) {
        (...)
    }

    (...)

}

But again, I would prefer a lot more if I could achieve the same via an interface without having to specify the type.
How can I accomplish that?

答案1

得分: 1

这是如何实现您最初的尝试的方法。如果我们在someFunction中调用传递的接口,那么someFunction必须是抽象的,因为在实践中我们不知道抽象类中的T是什么。

abstract class MyAbstractClass<T> {

    fun interface MyInterface<T> {
        suspend fun doSomething(param: T)
    }

    private var _callback: MyInterface<T>? = null

    abstract fun someFunction(callback: MyInterface<T>?) 

}

fun main() {
    val x = object: MyAbstractClass<String>() {
        override fun someFunction(callback: MyInterface<String>?) {
            runBlocking { callback?.doSomething("Hello world") }
        }
    }
    x.someFunction {
        println(it)
    }
}
英文:

Here’s how you can implement your original attempt to work. If we are invoking the passed interface in someFunction, then someFunction has to be abstract because in practice we don't know what T is in the abstract class.

abstract class MyAbstractClass&lt;T&gt; {

    fun interface MyInterface&lt;T&gt; {
        suspend fun doSomething(param: T)
    }

    private var _callback: MyInterface&lt;T&gt;? = null

    abstract fun someFunction(callback: MyInterface&lt;T&gt;?) 

}

fun main() {
    val x = object: MyAbstractClass&lt;String&gt;() {
        override fun someFunction(callback: MyInterface&lt;String&gt;?) {
            runBlocking { callback?.doSomething(&quot;Hello world&quot;) }
        }
    }
    x.someFunction {
        println(it)
    }
}

</details>



huangapple
  • 本文由 发表于 2023年6月19日 22:11:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定