<div dir="ltr">+100 to this.<div><br></div><div>I&#39;m not a huge fan of ~= syntax, but higher kinded types are something swift would benefit greatly from, particularly with stronger more concise type constraints, more code reuse.<br><br>Another example, one I&#39;m very keen on achieving, is this:<br><br>With Swift 1.2 how do you implement map on CollectionType that returns the same type of collection?</div><div><br></div><div>Give it a go, I learnt a lot failing! You&#39;re aiming to be able to do this:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">extension CollectionType {</font></div><div><font face="monospace, monospace">    func myMap ....</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">let myDictionary: [String:Int] = [&quot;test&quot;: 1]</font></div><div><font face="monospace, monospace">var otherDictionary = myDictionary.myMap { ($0, Float($1)) }</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">// does not fail:</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">assert(otherDictionary is [String:Float])</font></div><div><font face="monospace, monospace"><br></font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">let myArray: [Int] = [1]</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">// </font><span style="font-family:monospace,monospace">error: cannot convert &#39;[Float]&#39; to specified type &#39;[String : Float]&#39;</span></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><span style="font-family:monospace,monospace">otherDictionary</span><span style="font-family:monospace,monospace"> = myOptional.myMap { Float($0) }<br></span></blockquote><div><br>The caveats are:</div><div> *  it has to work regardless of what is implementing CollectionType, and</div><div> * it can&#39;t have an ambiguous return type</div><div><br></div><div>With higher kinded types you could do something like this:<br><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">extension CollectionType {</font></div><div><font face="monospace, monospace">    func myMap&lt;G: GeneratorType&gt;(transform: Generator.Element-&gt;G.Element) -&gt; Self&lt;G&gt; {</font></div><div><font face="monospace, monospace">        return Self(sequence: AnySequence(self).forEach(transform))</font></div><div><font face="monospace, monospace">    }</font></div><div><font face="monospace, monospace">}</font></div></blockquote><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 16, 2015 at 11:39 PM, Al Skipp via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><span class=""><blockquote type="cite">On 16 Dec 2015, at 11:11, Will Fancher via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><br>Hello,<br><br>A potent problem in my experience with Swift has been the lack of a way to generalize code that is repetitive on the basis of higher kinded types. For the uninitiated, higher kinded types are the ability to reason about generic types with their type parameters as variable. </blockquote><div><br></div></span><div>For those thinking, “Hmm… What’s the benefit of all this crazy sounding stuff?”, there’s a rather good practical talk about Functors.</div><div><br></div><b>How I Learned to Stop Worrying and Love the Functor:</b><div><a href="https://vimeo.com/132657092" target="_blank">https://vimeo.com/132657092</a></div><div><br></div><div><br></div><div>Here’s an example that is used:</div><div><br></div><div>func sayHello(name: Optional&lt;String&gt;) -&gt; Optional&lt;String&gt; {<br>  return name.map { &quot;Hello \($0)&quot; }<br>}<br><br>func sayHello(name: Array&lt;String&gt;) -&gt; Array&lt;String&gt; {<br>  return name.map { &quot;Hello \($0)&quot; }<br>}</div><div><br></div><div>It doesn’t use the usual syntax sugar for Optionals and Array, to make it clear that the only difference between the 2 functions is the type signature. If were possible to express the concept of a Functor, we could write one function that would accept both Optionals and Arrays as parameters – and not just those 2, but any Functor (Result, Future, Signal…).</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=pQw7h83fWt3LLbgkfL4TSUL0weaZnVFZxDe5GShw4uSCxtXSCGxlt6ZgBUdS9hYhrsMZ2XReYmzOip7f-2FplumlSzKJ3kZQAJueVkMfM-2Bp2XBSYJSmFmG5OjQ-2FYhdQNQKcAJUTxJn9E4JcEjnCUpugx36Uajq3C8qawWZyUhi89TirEg0Rvp-2Bbv09E-2FsF65bxuwylEgVyRzyRJtM7VwlaGw-3D-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>