Printing the Static Type of a Value

Your staticType still isn't quite right. What you want is:

func staticType<T>(of _: T) -> String {
    return "\(T.self)"
}

because type(of: value) in a generic context would still return the dynamic subclass of an object instance you pass in.

There are really two variants of type(of:). When you apply it to something known to be a protocol type, it opens the protocol type and gives you the type of the value inside as a protocol metatype. For cases where we don't see that a type is a protocol type, we can only provide the concrete metatype for the value. Inside the context of staticType, even if T == Error, the body of staticType can't see that, so it picks the latter meaning of type(of:).

2 Likes