英文:
How to override different types of generic type
问题
我保存我的操作类型在一个队列中,模板如下。例如,一个操作类型是 connectToBluetooth
,另一个操作类型是 sendMessageToBluetooth
。因此,每个操作应该能够返回不同类型的结果。
sealed class Resource<out T:Any>{
abstract val data:T?
data class Success<out T:Any> (override val data:T? = null):Resource<T>()
data class Error<out T:Any>(override val data:T? = null, val errorMessage:String? = null):Resource<T>()
data class Loading<out T:Any>(override val data:T? = null, val message:String? = null):Resource<T>()
}
abstract class OperationType {
abstract val result: ((Resource<String>) -> Unit)
}
data class Operation1(override val result: ((Resource<String>) -> Unit)): OperationType()
var nextPendingOperation: OperationType? = null
fun performOperation1(result: ((Resource<String>) -> Unit)) {
nextPendingOperation = Operation1(result)
}
fun main() {
performOperation1() {
}
var nextPendingOperation2 = nextPendingOperation
if ( nextPendingOperation2 is Operation1 ) {
nextPendingOperation2.result(Resource.Loading("message"))
}
}
但在上面的结构中,result
的 data
类型始终为 String
。下面的代码不起作用。如何使这段代码能够实现这个目的?
abstract class OperationType {
abstract val result: ((Resource<Any>) -> Unit)
}
data class Operation1(override val result: ((Resource<String>) -> Unit)): OperationType()
data class Operation2(override val result: ((Resource<Int>) -> Unit)): OperationType()
英文:
I save my operation types in a queue with the template below. For example one operation type is connectToBluetooth
, another operation type is sendMessageToBluetooth
. Because of this Each operation should be able to return a different type of result.
sealed class Resource<out T:Any>{
abstract val data:T?
data class Success<out T:Any> (override val data:T? = null):Resource<T>()
data class Error<out T:Any>(override val data:T? = null, val errorMessage:String? = null):Resource<T>()
data class Loading<out T:Any>(override val data:T? = null, val message:String? = null):Resource<T>()
}
abstract class OperationType {
abstract val result: ((Resource<String>) -> Unit)
}
data class Operation1(override val result: ((Resource<String>) -> Unit)): OperationType()
var nextPendingOperation: OperationType? = null
fun performOperation1(result: ((Resource<String>) -> Unit)) {
nextPendingOperation = Operation1(result)
}
fun main() {
performOperation1() {
}
var nextPendingOperation2 = nextPendingOperation
if ( nextPendingOperation2 is Operation1 ) {
nextPendingOperation2.result(Resource.Loading("message"))
}
}
But in the above structure, result
data
type is always string
. The code below does not working. How can i made this code to work for this purpose?
abstract class OperationType {
abstract val result: ((Resource<Any>) -> Unit)
}
data class Operation1(override val result: ((Resource<String>) -> Unit)): OperationType()
data class Operation2(override val result: ((Resource<Int>) -> Unit)): OperationType()
答案1
得分: 1
以下是翻译好的部分:
你可以让 `OperationType` 接受一个泛型参数,以便可以针对不同的资源类型执行不同的操作。
abstract class OperationType<T: Any> {
abstract val result: ((Resource<T>) -> Unit)
}
data class Operation1(override val result: ((Resource<String>) -> Unit)): OperationType<String>()
data class Operation2(override val result: ((Resource<Int>) -> Unit)): OperationType<Int>()
var nextPendingOperation: OperationType<*>? = null
fun performOperation1(result: ((Resource<String>) -> Unit)) {
nextPendingOperation = Operation1(result)
}
fun main() {
val nextPendingOperation2 = nextPendingOperation
when (nextPendingOperation2) {
is Operation1 -> nextPendingOperation2.result(Resource.Loading("message"))
is Operation2 -> nextPendingOperation2.result(Resource.Loading(1))
}
}
希望这对你有所帮助。如果你需要进一步的翻译或有其他问题,请随时提问。
英文:
You can make OperationType
accept a generic parameter, so that you can have different operations work on different resource types.
abstract class OperationType<T: Any> {
abstract val result: ((Resource<T>) -> Unit)
}
data class Operation1(override val result: ((Resource<String>) -> Unit)): OperationType<String>()
data class Operation2(override val result: ((Resource<Int>) -> Unit)): OperationType<Int>()
var nextPendingOperation: OperationType<*>? = null
fun performOperation1(result: ((Resource<String>) -> Unit)) {
nextPendingOperation = Operation1(result)
}
fun main() {
val nextPendingOperation2 = nextPendingOperation
when (nextPendingOperation2) {
is Operation1 -> nextPendingOperation2.result(Resource.Loading("message"))
is Operation2 -> nextPendingOperation2.result(Resource.Loading(1))
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论