<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><blockquote type="cite" class="">On Apr 20, 2016, at 1:15 PM, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""><br class="">on Tue Apr 19 2016, Thorsten Seitz <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""><blockquote type="cite" class="">I'd like to have something like Summable with 'add', 'adding' and 'zero' being a<br class="">separate protocol as well as somthing like Multiplicative with 'multiply',<br class="">'multiplied' and 'one' being a separate protocol, because these are universally<br class="">interesting for other cases, e.g. Summable would be useful for defining path<br class="">lengths in a graph library.<br class=""><br class="">Would you mind adding that to the proposal?<br class=""></blockquote><br class="">I suspect you may be headed into the realm of<br class="">protocols-as-bags-of-syntax.</blockquote><div class="">You say that like it’s a bad thing… Am I missing the point? (NOT a rhetorical question)</div><div class=""><br class=""></div><div class="">Speaking only for myself, I want stuff broken up into many simpler protocols (which `Arithmetic` and such conform to) because there are many algorithms which only require small parts of the larger protocol. What if I wanted to add a function that sums all the elements in a collection?</div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">CollectionType</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Generator.Element: Arithmetic {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> sum() -> </span><span style="font-variant-ligatures: no-common-ligatures" class="">Generator</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures" class="">Element</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> s = Generator.Element()</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> e </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> s.add(e)</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> s</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><br class=""></div><div class="">Yeah, it works, but if I want to get the sum of a collection of some custom type, I have to implement *all* of `Arithmetic`, as opposed to just the two parts of it (`init()` and `.add(:)`) that the algorithm actually uses. It’d be both simpler for the users and *more to the point of the function*, if it could be written like this:</div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">CollectionType</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">where</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> Generator.Element: <span style="color: rgb(112, 61, 170);" class="">Addable</span> {</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="color: rgb(0, 132, 0); font-variant-ligatures: no-common-ligatures;" class="">// "Addable" instead of "Arithmetic"</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> ... </span><span style="font-variant-ligatures: no-common-ligatures" class="">// No change here</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class="">}</div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div class="">Now, if Swift allowed you to add protocol conformance to protocols:</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Addable {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> ... </span><font color="#008400" face="Menlo" class="">// Relevant subset of `Arithmetic`</font></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class="">}</div><div class=""><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">#for</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> T </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">in</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> <span style="color: rgb(112, 61, 170);" class="">Arithmetic</span> { </span><span style="color: rgb(0, 132, 0); font-family: Menlo;" class="">// "#for" because this is clearly macro-ish, and # seems to be what we've settled on for that</span></div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class=""> : </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Addable</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {} </span><span style="color: rgb(0, 132, 0);" class="">// Don't need anything here since `Arithmetic` already has everything in `Addable`</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class="">… then this all becomes a somewhat moot point. If some generic function only needs a subset of a protocol’s functionality, said function’s author could do this “#for T …” song & dance, and their function would be defined with the minimum constraints.</div><div class=""><br class=""></div><br class=""><blockquote type="cite" class="">Look at pages 14 and 24 of<br class=""><<a href="http://www.cs.indiana.edu/pub/techreports/TR638.pdf" class="">http://www.cs.indiana.edu/pub/techreports/TR638.pdf</a>>—which does the job<br class="">right for C++—and you'll see why.</blockquote>COOL!!! Thanks for posting that, it looks like it’ll be a great read! :-D<div class=""><br class=""></div><div class="">- Dave Sweeris</div></body></html>