An Implementation Model for Rational Protocol Conformance Behavior

Protocol conformance gets really weird when there are conflicting conformances imported into a module.

Expanding on the uniqueXs example, starting with the fully-implemented version that you posted. Let's add a second file to Module D, as follows:

func whichConformanceGetsCalled() {
  print( X(y: 0) == X(y: 1) )
}

Should that compile? No, because X is not visible in the second file. So, let's modify it to import A to make X visible:

import A
func whichConformanceGetsCalled() {
  print( X(y: 0) == X(y: 1) )
}

Should that compile? I'm not sure. The == needs an implementation that takes operands of type X.

Are either of the conformances of X to Equatable visible in this second file? Perhaps, the conformances to Equatable should not be visible, because they have not been imported, and so that should not compile.

If that does compile, which conformance to Equatable should be used? I posit that it is indeterminate, and so an ambiguity error should be raised on the == operator at compile time (assuming that it hadn't already failed on account of the absence of a conformance to Equatable).

[FWIW: Under the current implementation, that actually does compile. Making X visible by importing A appears to drag into the visible scope at least one of the Equatable conformances. In my case, it happened to be the one from Module B, and it appeared that that conformance was used regardless of the order of the imports in the first file in Module D.]

5 Likes