<div dir="ltr">I am for this but I am sure that we won't get it in Swift 3. I do hope that we at least consider it enough to ensure that the ABI supports it to some extent.<div><br></div><div>TJ</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 16, 2015 at 8:36 AM, Andrew Bennett via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">+100 to this.<div><br></div><div>I'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'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'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] = ["test": 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 '[Float]' to specified type '[String : Float]'</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'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<G: GeneratorType>(transform: Generator.Element->G.Element) -> Self<G> {</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"><div><div class="h5">On Wed, Dec 16, 2015 at 11:39 PM, Al Skipp via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div style="word-wrap:break-word"><span><blockquote type="cite">On 16 Dec 2015, at 11:11, Will Fancher via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> 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<String>) -> Optional<String> {<br> return name.map { "Hello \($0)" }<br>}<br><br>func sayHello(name: Array<String>) -> Array<String> {<br> return name.map { "Hello \($0)" }<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></div></div><span class="">_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">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></span></blockquote></div><br></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=3FGfocPbgxkNkmje7djckg9Iw-2BGYY3X6RxJ1tkUXKCqYaqYIQqJ-2BHAq2G56Aybbqpey-2BGTTB-2BXvpmsimqiudUdfS9PAVWQQs-2FUN7qw7w36oy-2FC4OwHQw5dFY52isXFhLPAK0VS4zuQcm-2BNmS0eVLIgk2pvqBYbt7sZ4SOgk-2Fvsc00-2FY96PNmbWstqr6BAEvYsm8lUyOQfqQILflNG5FQ2gtZGh0vlND8Z-2Fg7NKOhjuk-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">
<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>