如何重写不同类型的泛型类型

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

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"))
    }
}

但在上面的结构中,resultdata 类型始终为 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&lt;out T:Any&gt;{
    abstract val data:T?
    data class Success&lt;out T:Any&gt; (override val data:T? = null):Resource&lt;T&gt;()
    data class Error&lt;out T:Any&gt;(override val data:T? = null, val errorMessage:String? = null):Resource&lt;T&gt;()
    data class Loading&lt;out T:Any&gt;(override val data:T? = null, val message:String? = null):Resource&lt;T&gt;()
}

abstract class OperationType {
	abstract val result: ((Resource&lt;String&gt;) -&gt; Unit)
}

data class Operation1(override val result: ((Resource&lt;String&gt;) -&gt; Unit)): OperationType()

var nextPendingOperation: OperationType? = null
fun performOperation1(result: ((Resource&lt;String&gt;) -&gt; Unit)) {
    nextPendingOperation = Operation1(result) 
}

fun main() {
    performOperation1() {
        
    }
    
    var nextPendingOperation2 = nextPendingOperation
    if ( nextPendingOperation2 is Operation1 ) {
        nextPendingOperation2.result(Resource.Loading(&quot;message&quot;))
    }
}

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&lt;Any&gt;) -&gt; Unit)
}

data class Operation1(override val result: ((Resource&lt;String&gt;) -&gt; Unit)): OperationType()
data class Operation2(override val result: ((Resource&lt;Int&gt;) -&gt; 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&lt;T: Any&gt; {
    abstract val result: ((Resource&lt;T&gt;) -&gt; Unit)
}

data class Operation1(override val result: ((Resource&lt;String&gt;) -&gt; Unit)): OperationType&lt;String&gt;()
data class Operation2(override val result: ((Resource&lt;Int&gt;) -&gt; Unit)): OperationType&lt;Int&gt;()

var nextPendingOperation: OperationType&lt;*&gt;? = null
fun performOperation1(result: ((Resource&lt;String&gt;) -&gt; Unit)) {
    nextPendingOperation = Operation1(result)
}

fun main() {
    val nextPendingOperation2 = nextPendingOperation
    when (nextPendingOperation2) {
        is Operation1 -&gt; nextPendingOperation2.result(Resource.Loading(&quot;message&quot;))
        is Operation2 -&gt; nextPendingOperation2.result(Resource.Loading(1))
    }
}

huangapple
  • 本文由 发表于 2023年4月13日 16:48:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003464.html
匿名

发表评论

匿名网友

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

确定