Is there a way to convert between an existential type and the existential metatype of the same protocol, statically?

For example, is there any way to combine both of these associated types into one, and base the other off of it? I don't think so.

protocol P { }

protocol EP {
  associatedtype AnyP
  associatedtype AnyPType
}

enum E: EP {
  typealias AnyP = any P
  typealias AnyPType = any P.Type
}

Couldn't you say

func f(anyP: any P) {
  let anyPType = type(of: anyP)
}

?

1 Like

Of course. Thank you. I did a bad job simplifying my problem and have edited to ask what I actually needed to know, now.

That's right, there is no way to produce an any P.Type from a T.Type by substitution; you'll always get an (any P).Type. The type(of:) operation in the expression type checker does this as a special case, but the type of type(of:) cannot be expressed in the type system.

3 Likes