pnizzle
pnizzle

Reputation: 6431

Error when using type of inside function that takes type parameter

Why is it that when using type(of:) inside a simple function, there are no issue. But once the function changes to take in a generic parameter T, type(of:) spoils itself? Even though there is nothing in the function signature that should affect it.

What is in function2 signature that is making type(of: ) fail?

enter image description here

Upvotes: 1

Views: 62

Answers (1)

Sweeper
Sweeper

Reputation: 273565

When you say type, Swift thought that you meant the function's parameter type, but you actually meant the type(of:) function.

You can either specify the module name (Swift) of the type(of:) function to differentiate:

Swift.type(of: controller)

or rename the parameter to something else:

public func function2<T>(t: T.Type, controller: UIViewController) -> Any
                         ^

Upvotes: 1

Related Questions