ubuntudroid
02/14/2023, 2:14 PMsealed class Resource<out T : Any>(open val data: T? = null) {
data class Success<out T : Any>(override val data: T) : Resource<T>()
class Failed<out T : Any>(val msg: String, val cause: Throwable? = null, data: T? = null) : Resource<T>(data)
class Loading<out T : Any>(data: T? = null) : Resource<T>(data)
}
I also have a mapper function with allows mapping the data type to something else:
fun <T : Any, R : Any> Resource<T>.map(mapper: (T) -> R): Resource<R> = ...
This works great on the Kotlin end of things. But I’m loosing all generic information on the Swift end in the mapper function. It seems to just be converted to (Any) -> Any
which means I need to constantly cast to get what I want which makes the code look really ugly.
I read through the official docs and understand, that this is expected. But is there a better way to handle generic and/or generic resources to make them more Swift compatible?Shubham Singh
02/14/2023, 2:25 PMubuntudroid
02/14/2023, 2:27 PMbnvinay92
02/14/2023, 4:06 PMubuntudroid
02/14/2023, 4:18 PMubuntudroid
02/14/2023, 4:20 PMbnvinay92
02/14/2023, 4:22 PMclass Mapper<R>(mapper: (MyClass) -> R)
with fun map(myClass: MyClass): R
bnvinay92
02/14/2023, 4:24 PMAny
ubuntudroid
02/14/2023, 4:25 PMbnvinay92
02/14/2023, 4:33 PMubuntudroid
02/14/2023, 4:35 PMubuntudroid
02/14/2023, 4:50 PMubuntudroid
02/14/2023, 5:22 PMubuntudroid
02/14/2023, 5:29 PMubuntudroid
02/15/2023, 7:45 PM