Checking whether a metatype is hashable

Swift has a way to check whether a metatype is compatible with a supertype or protocol:

X.self is Super.Type

However, this test is not possible when the supertype or protocol conformance in question is a protocol with associated types:

X.self is Hashable.Type // or Hashable.Protocol

Without having an instance value, is there any way to check whether a given metatype conforms to a given protocol if it has associated type requirements?

This is already supported in the upcoming version of Swift (thanks to what's already been implemented of SE-0309):

print(String.self is Hashable.Type)
// true
// warning: 'is' test is always true
// warning: protocol 'Hashable' as a type must be explicitly marked as 'any'

Huh, that warning makes less sense for is. Not sure it’s worth making the rules different than for as? though.

1 Like