Skip to content

Can provided members satisfy other protocols? #61

@LeaVerou

Description

@LeaVerou

This is somewhat related to #23 and #60.

Consider this:

protocol A {
	requires a;
}

protocol B {
	[A.a] () { /* elided */ }
}

Which of the following throws, if any?

class C1 implements A, B {}
class C2 implements B, A {}
  1. Both throw
  2. C1 throws
  3. C2 throws
  4. None of them throw

Basically:

  • Can provided members of one protocol satisfy required members of another protocol applied on the same class?
  • If so, does the order of application matter?

If I’m reading things right, it appears that the current behavior is that protocols are checked+implemented sequentially, as if we were independently calling Protocol.implement(), in which case C1 would throw. While simple to spec, this does not seem optimal for authors.

I wonder if a slightly more intuitive behavior might be that protocols are first (conceptually) flattened, with provided members of one protocol being able to satisfy required members of another, and the host class only has to satisfy what remains. In that case, none of them would throw.

With that change, these would be equivalent (see #60):

protocol A {
	requires a;
	foo() { { /* elided */ }
}

protocol B {
	[A.a] () { /* elided */ }
}

class C implements A, B {}
protocol A {
	requires a;
}

protocol B implements A {
	[A.a] () { /* elided */ }
}

class C implements B {}

cc @michaelficarra @ljharb

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions