Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generic parameter 'T' is not used in function signature

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading