Checking protocol conformances with metatypes

I'm trying to write a function that takes types as inputs and tests for conformances. Here's a simplified example:

let x = 3

protocol Test {}
extension Int: Test {}

x is Test // true
Int.self is Test.Type // true

func testing1<T>(type: T.Type) -> Bool {
  return type is Test.Type
}

func testing2<T, P>(type: T.Type, protocol: P.Type) -> Bool {
  return type is P.Type
}

testing1(type: Int.self) // true
testing2(type: Int.self, protocol: Test.self) // false

Any ideas how I can fix this?

4 Likes

Hate bumping an old thread, but I would also want to know if there is a swift construct that would result in swift_conformsToProtocol being called inside testing2.

I would also love to have an answer to this fundamental. It's quite surprising to me that is should fail to check conformance to protocol metatypes.

Oddly:

Int.self is Test.Type // true, but
Int.self is Test.Protocol // false

This surfaces real and problematic run-time consequences.