[swift-evolution] [Pitch][Bug?] Test for Anti-Conformance During Generics

Daryle Walker darylew at mac.com
Sun Jul 30 15:26:04 CDT 2017

[Apologies if this is in Swift 4 already.]

We can have generic parameters match a protocol. But I have an idea in my head that involves the type NOT conforming to a protocol. I don’t think we have a way currently to work with this.

In C++, templates can be specialized or partially specialized. Either specialization lets you define special code when the template parameters match the pattern. I realized that it can anti-match by putting your secret sauce in the general version of the template and leave the specialized version empty. But, AFAIK, we can’t specialize Swift generics (in the same way), so we’re stuck.

The change needs to be added to generic parameter conformance in parameter and where clauses. We should also support an anti-version for same-type requirements.

Now the question is what token (sequence) to use? For anti-conformance to same-type, I don’t see a reason to not use the obvious “!=“ choice. But what would be the opposite of the colon used in type/protocol matching? Would “!:” look dumb? Would we have to make sure a “T!” type expression isn’t against the “:” to prevent the parser from changing conformance to anti-conformance?

Would putting a “!” directly before the type/protocol to be matched (i.e. on the right of the colon) be better?

Grammar of a Generic Parameter Clause

generic-parameter-clause → < generic-parameter-list >
‌generic-parameter-list → generic-parameter | generic-parameter , generic-parameter-list
‌generic-parameter → type-name
‌generic-parameter → type-name : !opt type-identifier
‌generic-parameter → type-name : !opt protocol-composition-type
‌generic-where-clause → where requirement-list
‌requirement-list → requirement | requirement , requirement-list
‌requirement → conformance-requirement | same-type-requirement
‌conformance-requirement → type-identifier : !opt type-identifier
‌conformance-requirement → type-identifier : !opt protocol-composition-type
‌same-type-requirement → type-identifier == type
‌same-type-requirement → type-identifier != type

Where the fourth, fifth, ninth, and tenth productions add an optional “!”. And the last production is new (although with a misleading title).


Now reported at <https://bugs.swift.org/browse/SR-5588>.

Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170730/3cbe1202/attachment.html>

More information about the swift-evolution mailing list