[swift-evolution] [Proposal] More Powerful Constraints for Associated Types

Thorsten Seitz tseitz42 at icloud.com
Mon Apr 25 00:08:29 CDT 2016

I've been missing this myself


> Am 25.04.2016 um 04:15 schrieb Rod Brown via swift-evolution <swift-evolution at swift.org>:
> I'm definitely a supporter of this change. It addresses things I've hit multiple times.
>> On 25 Apr 2016, at 6:34 AM, David Hart via swift-evolution <swift-evolution at swift.org> wrote:
>> I wrote the proposal which was discussed to introduce generic constraints for associated types. I’d like to get some feedback on it and get it ready before submitting it:
>> More Powerful Constraints for Associated Types
>> Proposal: SE-XXXX
>> Author(s): David Hart
>> Status: TBD
>> Review manager: TBD
>> Introduction
>> This proposal seeks to introduce a where expression to associated types declarations to bring the same expressive power as generic type constraints.
>> This proposal was discussed on the Swift Evolution list in the [swift-evolution] [Completing Generics] Arbitrary requirements in protocols thread.
>> Motivation
>> Currently, associated type declarations can only express simple inheritance constraints and not the more sophisticated constraints available to generic types with the where expression. Some designs, including many in the Standard Library, require more powerful constraints for associated types to be truly elegant. For example, the SequenceType protocol can be declared as follows:
>> protocol Sequence {
>>     associatedtype Iterator : IteratorProtocol
>>     associatedtype SubSequence : Sequence where SubSequence.Iterator.Element == Iterator.Element
>>     ...
>> }
>> Detail Design
>> With this proposal, the grammar for protocols associated types would be modified to:
>> protocol-associated-type-declaration → attributesopt access-level-modifieropt associatedtype typealias-name ­type-inheritance-clause­opt­ typealias-assignment­opt requirement-clauseopt
>> The new requirement-clause is then used by the compiler to validate the associated types of conforming types.
>> Issues
>> Douglas Gregor argues that the proposed syntax is redundant when adding new constraints to an associated type declared in a parent protocol and proposes another syntax: 
>> protocol Collection : Sequence {
>>     where SubSequence : Collection
>> } 
>> But as Douglas notes himself, that syntax will become ambiguous if we adopt the generic where expression at the end of declarations like discussed in the following thread: http://thread.gmane.org/gmane.comp.lang.swift.evolution/13886/focus=14058. For those reasons, it might be wiser not to introduce the shorthand syntax.
>> Acknowledgements
>> Thanks to Dave Abrahams and Douglas Gregor for taking the time to help me through this proposal.
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> _______________________________________________
> 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/20160425/a157a9e4/attachment.html>

More information about the swift-evolution mailing list