[swift-evolution] Protocol non-conformance clause

Dave Abrahams dabrahams at apple.com
Sun May 1 19:14:43 CDT 2016


on Fri Apr 29 2016, Erica Sadun <swift-evolution at swift.org> wrote:

> Gmane is down as far as my browser is concerned and I haven't found anything by
> Googling.
>
> Given the following:
>
> protocol A {func foo()}
> protocol B {} // empty protocol
>
> extension A where Self:B {
> func foo() {
> print("Self is B")
> }
> }
>
> // Works
> struct S1: A, B {}
> S1().foo()
>
> Is there a way to produce a similar extension that exempts any type that
> conforms to B?

Here's one horrible answer:

  protocol A {
    func foo()
  }

  protocol B {} // empty protocol
  extension B {
    func foo() {
      fatalError("uncallable")
    }
  }

  extension A  {
    func foo() {
      print("Self is not B")
    }
  }

  // Works
  struct S0: A {
    func foo() {
      print("Self is S0")
    }
  }
  S0().foo()

  // Fails
  struct S1: A, B {}
  S1().foo()

I'm sure there are other awful things one could do, too. :-)

-- 
Dave



More information about the swift-evolution mailing list