How to decode data to generic type? #181
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Apologies for the late response on this. This is indeed a shortcoming in Kotlin's generics handling, which Skip doesn't have any good way of overcoming. Your best bet is to include the type itself as a parameter, even though it is redundant, similar to how we do it with // SKIP DECLARE: override fun <TResponse> send(type: KClass<TResponse>): TResponse where TResponse: Any
func send<TResponse>(_ type: TResponse.Type) throws -> TResponse where TResponse: Decodable {
return try self.impl.unwrap(as: type)
let (data, _) = try await URLSession. shared data(for: urlRequest)
return try JSONDecoder().decode(type, from: data)
} |
Beta Was this translation helpful? Give feedback.
-
something similar happened to me and I was able to solve it by implementing this piece of code: https://github.com/orgs/skiptools/discussions/11#discussioncomment-11249025 @inline(__always) func fetchData<T>(_ type: T.Type, endpoint: String) async throws -> T where T: Decodable {
...
do {
...
return try decoder.decode(type, from: data)
} catch {
...
}
} |
Beta Was this translation helpful? Give feedback.
something similar happened to me and I was able to solve it by implementing this piece of code: https://github.com/orgs/skiptools/discussions/11#discussioncomment-11249025