Scoped Conformances

I'll try to come back to the rest of this post later, but for now I just wanted to address this part. If you're saying, that exact code should not print "P", then I agree, because the X: P is not visible where X() was turned into an existential.

That said, if you put a public someX: Any = X() in File1.swift and then `foo(someX), IMO it should print "P". The situation is exactly analogous to this code, and it should behave the same:

// File1.swift
public class P { }
public struct X { }
internal class ConformanceXToP: P {
  init(_ value: X) { self.value = value }
  let value: X
}
public let someX: Any = ConformanceXToP(X())

// File2.swift
func foo(_ value: Any) {
  if let x = value as? P { print("P") }
}

foo(X())    // X is not a conformance to P, locally.  No output.
foo(someX)  // someX is a conformance to P.  Prints "P"

The example you gave doesn't expose the internal conformance X: P any more than my variant exposes the internal class ConformanceXToP.

1 Like