Is generic code is not supported in Skip #11
-
Trying to write below function in swift:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
The Skip transpiler supports many Swift generic constructs, but not everything. We are constrained by Kotlin's limitations, and in this case the limitations of reified type parameters on the JVM (which famously implements generics though type erasure, and thus cannot provide runtime information about the types based on the signature alone). This is one such limitation: you can't reference I think this might work: func fetch<T: Decodable>(typeParam: T.Self,
api: TMDBAPI
) async throws -> T {
let request = try api.getURLRequest()
let (data, _) = try await URLSession.shared.data(for: request)
let decodedData = try JSONDecoder().decode(typeParam, from: data)
return decodedData
} These generics limitations are mentioned in the Swift Support page, but we will expand on this in the coming days to discuss alternative implementations to work around generic limitations. |
Beta Was this translation helpful? Give feedback.
-
I think an example of doing a simple fetch with generics would be great to have in docs @marcprux |
Beta Was this translation helpful? Give feedback.
The Skip transpiler supports many Swift generic constructs, but not everything. We are constrained by Kotlin's limitations, and in this case the limitations of reified type parameters on the JVM (which famously implements generics though type erasure, and thus cannot provide runtime information about the types based on the signature alone).
This is one such limitation: you can't reference
T.self
when all you have is a type parameter: you'll need an instance, or the type itself, to be passed as a parameter.I think this might work: