英文:
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<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)
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论