<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hello, Swift community!<div class=""><br class=""></div><div class="">I'd like to start a discussion about a possibility of constrained protocol aliases. The declaration would look like this:</div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class=""><font color="#941751" class="">typealias</font> <font color="#008f00" class="">BinaryProtocol</font> = <font color="#008f00" class="">RandomAccessCollection</font> & <font color="#008f00" class="">MutablCollection</font> & <font color="#008f00" class="">RangeReplaceableCollection</font> <font color="#941751" class="">where</font> <font color="#008f00" class="">Binary</font>.<font color="#008f00" class="">Index</font> == <font color="#008f00" class="">Int</font>, <font color="#008f00" class="">Binary</font>.<font color="#008f00" class="">Element</font> == <font color="#008f00" class="">Bool</font></font></div></div><div class=""><br class=""></div><div class="">The syntax and semantics of this declaration are exactly the same as an analogous <font color="#941751" face="Menlo" class="">associatedtype</font> declaration inside a protocol.</div><div class="">In the example above, the type <font face="Menlo" color="#008f00" class="">BinaryProtocol</font> represents a logical array of bits and is a generic-only protocol that is usable in any context where an integer-indexed mutable range-replaceable random-access collection is expected.</div><div class="">Now, it can be used in a very concise and elegant way:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""><font color="#941751" class="">public</font> <font color="#941751" class="">protocol</font> <font color="#008f00" class="">BinaryInitializable</font> {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><font color="#941751" class="">init</font><<font color="#008f00" class="">Binary</font>>(binary: <font color="#008f00" class="">Binary</font>) <font color="#941751" class="">where</font> <font color="#008f00" class="">Binary</font>: <font color="#008f00" class="">BinaryProtocol</font></font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">which would otherwise look very verbose and inelegant:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""><font color="#941751" class="">public</font> <font color="#941751" class="">protocol</font> <font color="#008f00" class="">BinaryInitializable</font> {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><font color="#941751" class="">init</font><<font color="#008f00" class="">Binary</font>>(binary: <font color="#008f00" class="">Binary</font>) <font color="#941751" class="">where</font> Binary: <font color="#008f00" class="">RandomAccessCollection</font> & <font color="#008f00" class="">MutablCollection</font> & <font color="#008f00" class="">RangeReplaceableCollection,</font> <font color="#008f00" class="">Binary</font>.<font color="#008f00" class="">Index</font> == <font color="#008f00" class="">Int</font>, <font color="#008f00" class="">Binary</font>.<font color="#008f00" class="">Element</font> == <font color="#008f00" class="">Bool</font></font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><div class=""><br class=""></div><div class="">Considering that smaller sets of constraints could be aliased to their own protocol and then composited into more complex aliases, this feature would dramatically improve readability and maintainability of code that uses complex constraints, that currently leads to arcane mess:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""><font color="#941751" class="">struct</font> <font color="#008f00" class="">Mirror</font> {</font></div><div class=""><ul class="container" style="box-sizing: border-box; margin: 0px; padding: 0px; width: auto; list-style: none;"><li class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="font-family: 'Avenir Next'; white-space: pre;">        </span><font color="#797979" class="">/// ...</font></font></li><li class=""><font face="Menlo" class=""></font></li><li class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="font-family: 'Avenir Next'; white-space: pre;">        </span><font color="#941751" class="">init</font><<font color="#008f00" class="">Subject</font>, <font color="#008f00" class="">C</font> <font color="#941751" class="">where</font> <font color="#008f00" class="">C</font> : <font color="#008f00" class="">Collection</font>, <font color="#008f00" class="">C</font>.<font color="#008f00" class="">Indices</font> : <font color="#008f00" class="">Collection</font>, <font color="#008f00" class="">C</font>.<font color="#008f00" class="">SubSequence</font> : <font color="#008f00" class="">Collection</font>, <font color="#008f00" class="">C</font>.<font color="#008f00" class="">Indices</font>.<font color="#008f00" class="">Index</font> == <font color="#008f00" class="">C</font>.<font color="#008f00" class="">Index</font>, <font color="#008f00" class="">C</font>.<font color="#008f00" class="">Indices</font>.<font color="#008f00" class="">SubSequence</font> == <font color="#008f00" class="">C</font>.<font color="#008f00" class="">Indices</font>, <font color="#008f00" class="">C</font>.<font color="#008f00" class="">Iterator</font>.<font color="#008f00" class="">Element</font> == <font color="#008f00" class="">Mirror</font>.<font color="#008f00" class="">Child</font>, <font color="#008f00" class="">C</font>.<font color="#008f00" class="">SubSequence</font>.<font color="#008f00" class="">Index</font> == <font color="#008f00" class="">C</font>.<font color="#008f00" class="">Index</font>, <font color="#008f00" class="">C</font>.<font color="#008f00" class="">SubSequence</font>.<font color="#008f00" class="">Indices</font> : <font color="#008f00" class="">Collection</font>, <font color="#008f00" class="">C</font>.<font color="#008f00" class="">SubSequence</font>.<font color="#008f00" class="">SubSequence</font> == <font color="#008f00" class="">C</font>.<font color="#008f00" class="">SubSequence</font>, <font color="#008f00" class="">C</font>.<font color="#008f00" class="">Indices</font>.<font color="#008f00" class="">Iterator</font>.<font color="#008f00" class="">Element</font> == <font color="#008f00" class="">C</font>.<font color="#008f00" class="">Index</font>, <font color="#008f00" class="">C</font>.<font color="#008f00" class="">SubSequence</font>.<font color="#008f00" class="">Indices</font>.<font color="#008f00" class="">Index</font> == <font color="#008f00" class="">C</font>.<font color="#008f00" class="">Index</font>, <font color="#008f00" class="">C</font>.<font color="#008f00" class="">SubSequence</font>.<font color="#008f00" class="">Indices</font>.<font color="#008f00" class="">SubSequence</font> == <font color="#008f00" class="">C</font>.<font color="#008f00" class="">SubSequence</font>.<font color="#008f00" class="">Indices</font>, <font color="#008f00" class="">C</font>.<font color="#008f00" class="">SubSequence</font>.<font color="#008f00" class="">Iterator</font>.<font color="#008f00" class="">Element</font> == <font color="#008f00" class="">Mirror</font>.<font color="#008f00" class="">Child</font>, <font color="#008f00" class="">C</font>.<font color="#008f00" class="">SubSequence</font>.<font color="#008f00" class="">Indices</font>.<font color="#008f00" class="">Iterator</font>.<font color="#008f00" class="">Element</font> == <font color="#008f00" class="">C</font>.<font color="#008f00" class="">Index</font>>(_ subject: <font color="#008f00" class="">Subject</font>, children: <font color="#008f00" class="">C</font>, displayStyle: <font color="#008f00" class="">Mirror</font>.<font color="#008f00" class="">DisplayStyle</font>? = <font color="#941751" class="">default</font>, ancestorRepresentation: <font color="#008f00" class="">Mirror</font>.<font color="#008f00" class="">AncestorRepresentation</font> = <font color="#941751" class="">default</font>)</font></li><li class=""></li></ul><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><font face="Menlo" color="#797979" class="">/// ...</font></div></div><div class=""><font face="Menlo" class="">}</font></div></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><span style="font-family: Menlo;" class=""><br class=""></span></div><div class=""><font color="#797979" face="Menlo" class="">/// A collection that is its own sub-sequence</font></div><div class=""><font face="Menlo" class=""><font color="#941751" class="">typealias</font> <font color="#008f00" class="">RecursivelySliceableCollection</font> = <font color="#008f00" class="">Collection</font> <font color="#941751" class="">where</font></font></div><div class=""><span class="Apple-tab-span" style="font-family: Menlo; white-space: pre;">        </span><font color="#008f00" face="Menlo" class="">RecursivelySliceableCollection</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">SubSequence</font><font face="Menlo" class="">: <font color="#008f00" class="">Collection</font>,</font></div><div class=""><span style="font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span></span><font color="#008f00" face="Menlo" class="">RecursivelySliceableCollection</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">SubSequence</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">Element</font><span style="font-family: Menlo;" class=""> == </span><font color="#008f00" face="Menlo" class="">RecursivelySliceableCollection</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">Element</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></font><font color="#008f00" face="Menlo" class="">RecursivelySliceableCollection</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">SubSequence</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">Indices</font><span style="font-family: Menlo;" class=""> == </span><font color="#008f00" face="Menlo" class="">RecursivelySliceableCollection</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">Indices</font><span style="font-family: Menlo;" class="">,</span></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span></span><font color="#008f00" face="Menlo" class="">RecursivelySliceableCollection</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">SubSequence</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">SubSequence</font><span style="font-family: Menlo;" class=""> == </span><font color="#008f00" face="Menlo" class="">RecursivelySliceableCollection</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">SubSequence</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font color="#797979" face="Menlo" class="">/// A collection that is its own index collection</font></div><div class=""><font color="#941751" face="Menlo" class="">typealias</font><span style="font-family: Menlo;" class=""> </span><font color="#008f00" face="Menlo" class="">RecursivelyIndexableCollection</font><span style="font-family: Menlo;" class=""> = </span><font color="#008f00" face="Menlo" class="">Collection</font><span style="font-family: Menlo;" class=""> </span><font color="#941751" face="Menlo" class="">where</font></div><div class=""><span style="font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><font color="#008f00" face="Menlo" class="">RecursivelyIndexableCollection</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">Indices</font><span style="font-family: Menlo;" class=""> == </span><font color="#008f00" face="Menlo" class="">RecursivelySliceableCollection</font><span style="font-family: Menlo;" class="">,</span></div><div class=""><span style="font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><font color="#008f00" face="Menlo" class="">RecursivelyIndexableCollection</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">Indices</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">Index</font><span style="font-family: Menlo;" class=""> == </span><font color="#008f00" face="Menlo" class="">RecursivelyIndexableCollection</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">Index</font><span style="font-family: Menlo;" class="">,</span></div><div class=""><span style="font-family: Menlo;" class=""><br class=""></span></div><div class=""><font color="#941751" face="Menlo" class="">struct</font><span style="font-family: Menlo;" class=""> Mirror {</span></div><div class=""><span class="Apple-tab-span" style="font-family: Menlo; white-space: pre;">        </span><font color="#797979" face="Menlo" class="">/// ...</font></div><div class=""><span class="Apple-tab-span" style="font-family: Menlo; white-space: pre;">        </span><font color="#941751" face="Menlo" class="">init</font><span style="font-family: Menlo;" class=""><</span><font color="#008f00" face="Menlo" class="">Subject</font><span style="font-family: Menlo;" class="">, </span><font color="#008f00" face="Menlo" class="">C</font><span style="font-family: Menlo;" class="">: </span><font color="#008f00" face="Menlo" class="">RecursivelySliceableCollection</font><span style="font-family: Menlo;" class=""> & </span><font color="#008f00" face="Menlo" class="">RecursivelyIndexableCollection</font><span style="font-family: Menlo;" class="">, </span><font color="#941751" face="Menlo" class="">where</font> <font color="#008f00" face="Menlo" class="">C</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">Element</font><span style="font-family: Menlo;" class=""> == </span><font color="#008f00" face="Menlo" class="">Mirror</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" face="Menlo" class="">Child</font><span style="font-family: Menlo;" class="">>(</span><span style="font-family: Menlo;" class="">_ subject:</span><span style="font-family: Menlo;" class=""> </span><font color="#008f00" style="font-family: Menlo;" class="">Subject</font><span style="font-family: Menlo;" class="">, children:</span><span style="font-family: Menlo;" class=""> </span><font color="#008f00" style="font-family: Menlo;" class="">C</font><span style="font-family: Menlo;" class="">, displayStyle:</span><span style="font-family: Menlo;" class=""> </span><font color="#008f00" style="font-family: Menlo;" class="">Mirror</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" style="font-family: Menlo;" class="">DisplayStyle</font><span style="font-family: Menlo;" class="">? =</span><span style="font-family: Menlo;" class=""> </span><font color="#941751" style="font-family: Menlo;" class="">default</font><span style="font-family: Menlo;" class="">, ancestorRepresentation:</span><span style="font-family: Menlo;" class=""> </span><font color="#008f00" style="font-family: Menlo;" class="">Mirror</font><span style="font-family: Menlo;" class="">.</span><font color="#008f00" style="font-family: Menlo;" class="">AncestorRepresentation</font><span style="font-family: Menlo;" class=""> </span><span style="font-family: Menlo;" class="">=</span><span style="font-family: Menlo;" class=""> </span><font color="#941751" style="font-family: Menlo;" class="">default</font><span style="font-family: Menlo;" class="">)</span></div><div class=""><span class="Apple-tab-span" style="font-family: Menlo; white-space: pre;">        </span><font color="#797979" face="Menlo" class="">/// ...</font></div><div class=""><span style="font-family: Menlo;" class="">}</span></div><div class=""><br class=""></div><div class="">Even considering that the proposal SE-0157 (<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0157-recursive-protocol-constraints.md" class="">https://github.com/apple/swift-evolution/blob/master/proposals/0157-recursive-protocol-constraints.md</a>) is going to make this specific use case a non-issue, the principle applies to all cases where there are commonly used complex constraints that don't necessarily involve recursive constraints.</div><div class=""><br class=""></div><div class=""><b class="">Specializing Generic-Only Protocols For Non-Generic Use</b></div><div class=""><br class=""></div><div class="">An additional feature that would prove to be very useful would be to make a constrained protocol alias be a non-generic-only protocol if the constraints of the alias declaration specify a same-type requirement for all its associated types, while defaulted associated types would also count.</div><div class="">Example:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""><font color="#941751" class="">protocol</font> <font color="#008f00" class="">Consumer</font> {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><font color="#941751" class="">associatedtype</font> <font color="#008f00" class="">Consumable</font></font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><font color="#941751" class="">mutating</font> <font color="#941751" class="">func</font> consume(_ consumable: <font color="#008f00" class="">Consumable</font>) <font color="#941751" class="">throws</font></font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><font color="#941751" class="">var</font> consumer0: <font color="#008f00" class="">Consumer</font> <font color="#797979" class="">// </font></font><span style="font-family: Menlo;" class=""><i class=""><font color="#ff2600" class="">error: Consumer is only usable in a generic context</font></i></span></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><font color="#941751" class="">typealias</font> <font color="#008f00" class="">CharacterConsumer</font> = <font color="#008f00" class="">Consumer</font> <font color="#941751" class="">where </font> </font><span style="color: rgb(0, 143, 0); font-family: Menlo;" class="">Character</span><font face="Menlo" class=""><font color="#008f00" class="">Consumer</font>.<font color="#008f00" class="">Consumable</font> == </font><span style="color: rgb(0, 143, 0); font-family: Menlo;" class="">Character</span></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><font color="#941751" class="">var</font> consumer1: </font><span style="color: rgb(0, 143, 0); font-family: Menlo;" class="">Character</span><font face="Menlo" class=""><font color="#008f00" class="">Consumer</font> <font color="#797979" class="">// OK</font></font></div><div class=""><br class=""></div><div class="">The current workaround would be to declare a new protocol with protocol inheritance clauses and a <font color="#941751" face="Menlo" class="">where</font> clause, but the major downside is that it introduces a completely new protocol that is not compatible with any context that expects the underlying protocols and their constraints.</div><div class=""><br class=""></div><div class="">Regards,</div><div class="">Gor Gyolchanyan.</div><div class=""><br class=""></div></body></html>