Lifting the "Self or associated type" constraint on existentials

It's awkward, but you can in fact "open" the existential by invoking a protocol extension method, which will give you Self as the dynamic type of the value inside the method, which you can then pass to generic functions:

func foo<T: P>(_: T)

extension P {
  func _forwardToFoo() { foo(self) }
}

func callFooOnEach(_ ps: [P]) {
  for p in ps {
    p._forwardToFoo()
  }
}

Another nice stepping stone on the way to generalized existentials (which I'm not proposing immediately) would be to make it so that existentials automatically open when passed as generic arguments to functions. Oftentimes this is closer to what you want than protocol self-conformance, since you really want to operate on the value inside the existential, not the existential itself.

30 Likes