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

Dmitri Gribenko gribozavr at gmail.com
Fri Apr 29 02:36:13 CDT 2016


On Fri, Apr 29, 2016 at 12:25 AM, David Hart via swift-evolution
<swift-evolution at swift.org> wrote:
> I’ve taken some time to digest the current feedback and I’ve changed my mind. The syntax for adding constraints to a sub-protocol in the protocol’s definition where clause is starting to grow on me. Before I modify the proposal, I'd still like to understand something:
>
> What is the use of declaring a default associated types with the `=` syntax in protocols? I’ve never used them and I don’t understand what they provide.

Please take a look at the Collection protocol
(https://github.com/apple/swift/blob/master/stdlib/public/core/Collection.swift).

associatedtype Iterator : IteratorProtocol = IndexingIterator<Self>
associatedtype SubSequence : IndexableBase, Sequence = Slice<Self>
associatedtype Indices : IndexableBase, Sequence = DefaultIndices<Self>

Some collections want to customize these, but for simple ones the
defaults are usually fine.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-evolution mailing list