[swift-evolution] Protocol non-conformance clause

Xiaodi Wu xiaodi.wu at gmail.com
Fri Apr 29 12:51:48 CDT 2016


Yes, certainly this works today. The motivation for Erica's question is
that this would not work without modifying the third-party code if keywords
were required to indicate implementation of protocol requirements.

One possible solution might emerge if it is possible to extend a protocol
conditional on Self not being some concrete type. Thus, asking whether
there is a way to express that.
On Fri, Apr 29, 2016 at 12:45 Thorsten Seitz <tseitz42 at icloud.com> wrote:

> No problem, that still works, because the most specific implementation is
> chosen:
>
> protocol A { func foo()}
> protocol B {} // empty protocol
>
> extension A  {
>     func foo() {
>         print("Self is A")
>     }
> }
>
> extension A where Self: B {
>     func foo() {
>         print("Self is B")
>     }
> }
>
> // Works
> struct S1: A, B {}
> S1().foo() // Self is B
>
> struct S2: A {}
> S2().foo() // Self is A
>
>
> // Wu's example works, too
>
> struct ThirdParty {
>     func foo() {
>         print("Self is ThirdParty")
>     }
> }
>
> extension ThirdParty : A {}
>
> ThirdParty().foo() // Self is ThirdParty
>
>
> // dynamic dispatch works, too
>
> let a1: A = S1()
> a1.foo() // Self is B
>
> let a2: A = S2()
> a2.foo() // Self is A
>
> let a3: A = ThirdParty() // Self is ThirdParty
> a3.foo()
>
>
> -Thorsten
>
>
> Am 29.04.2016 um 17:20 schrieb Erica Sadun <erica at ericasadun.com>:
>
> In Wux's example, he has third party code:
>
> ```
> Type ThirdParty {
>     func foo() { print("from third party") }
> }
> ```
>
> Then in his own code, he defines protocol A and extends it:
>
> extension A {
>     func foo() {
>         print("Self is B")
>     }
> }
>
> and conforms ThirdParty to A. But he wants the original  foo()
> implementation. Your approach
> for writing an extension for plain A without a where clause doesn't offer
> that solution. The goal
> here is "Add this default behavior *only* where a type does not conform to
> B"
>
> -- E
>
>
> On Apr 29, 2016, at 9:10 AM, Thorsten Seitz <tseitz42 at icloud.com> wrote:
>
> Just writing an extension for plain A without a where clause works.
>
> -Thorsten
>
> Am 29.04.2016 um 16:03 schrieb Erica Sadun via swift-evolution <
> swift-evolution at swift.org>:
>
> 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?
>
> cc'ing in Wux because this is a direct response to a scenario he brought
> up yesterday.
>
> -- E
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160429/f1889b22/attachment.html>


More information about the swift-evolution mailing list