Cdev
03/04/2021, 7:55 PMinline fun <reified T : Any> jsonToConfig(stringJson: String): T? =
Json.decodeFromString<T?>(stringJson)
Using Kotlinx Serialization decoding from the string works fine in Kotlin. But in Swift:
let projectConfig: ProjectConfig? = config.jsonToConfig<ProjectConfig>(stringJson: jsonString)
I get the following error: "Cannot explicitly specialize a generic function". Any ideas how I could go about getting this to work?Anders
03/04/2021, 8:45 PMobj.foo<Type1, Type2>()
. But most importantly, there is no concept of "generic functions" in Objective-C, only generic classes.
So if you inspect the function signature which Swift imports from Objective-C, you will see it isn't a generic function at all.
like
class FileNameKt {
class func jsonToConfig(string: String): Any?
}
and I am not sure what will happen when you call it. Undefined behaviour likely.
You will either need to define a non-generic version that calls the generic version, or wrapping the function into a generic class if you do need to have it available in its generic form in ObjC/Swift.
fun jsonToMyConfig(string: String): MyConfig? = jsonToConfig<MyConfig>(string)
or
class Converter<T: Any> {
fun convert(string: String): T? = jsonToConfig<T>(string)
}
Cdev
03/04/2021, 9:09 PMlouiscad
03/07/2021, 1:13 PMCdev
03/09/2021, 2:25 PM