I want to associate function’s generic type to other class inside the function.
I get error Generic parameter 'T' is not used in function signature for code below.
func someFunc<T: Codable>() {
let nmyClass = MyClass<T>()
}
How can I make the error go away without returning T?
When I add return T? from the function error goes away but since I don’t need to return anything, it is kid of odd.
func someFunc<T: Codable>() -> T? {
let nmyClass = MyClass<T>()
return nil
}
>Solution :
You generally would pass the type here:
func someFunc<T: Codable>(for: T.Type) {
let nmyClass = MyClass<T>()
}
You would then call it as:
someFunc(for: Int.self)