<html><body><div><br></div><div><br>Am 15. März 2016 um 01:51 schrieb Dave via swift-evolution <swift-evolution@swift.org>:<br><br><div><blockquote type="cite"><div class="msg-quote" style="word-wrap: break-word;" data-mce-style="word-wrap: break-word;"><br class=""><blockquote type="cite" class="">On Mar 14, 2016, at 5:51 PM, Dmitri Gribenko via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="" data-mce-href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br class=""><br class="">Optional.map returns an Optional.<br class=""><br class="">Array.map returns an Array.<br class="">Set.map returns an Array.<br class=""><any other collection>.map returns an Array.<br class=""><br class="">I can't say that it is not valid to think about an Optional as a tiny<br class="">collection, but as implemented in Swift, .map() does objectively<br class="">behave differently...<br class=""></blockquote><div class="">That behavior is, at least partially, because protocols don’t currently support this:</div><div class=""><div style="margin: 0px; line-height: normal;" class="" data-mce-style="margin: 0px; line-height: normal;"><div style="font-family: 'menlo'; margin: 0px; line-height: normal;" class="" data-mce-style="font-family: 'menlo'; margin: 0px; line-height: normal;"><span style="color: #bb2ca2;" class="" data-mce-style="color: #bb2ca2;">protocol</span><span class=""> CollectionType {</span></div><div style="font-family: 'menlo'; margin: 0px; line-height: normal; color: #bb2ca2;" class="" data-mce-style="font-family: 'menlo'; margin: 0px; line-height: normal; color: #bb2ca2;"><span style="color: #000000;" class="" data-mce-style="color: #000000;"> </span><span class="">typealias</span><span style="color: #000000;" class="" data-mce-style="color: #000000;"> T</span></div><div style="font-family: 'menlo'; margin: 0px; line-height: normal;" class="" data-mce-style="font-family: 'menlo'; margin: 0px; line-height: normal;"><span class=""> </span><span style="color: #bb2ca2;" class="" data-mce-style="color: #bb2ca2;">func</span><span class=""> map<U>(transform: </span><span style="color: #703daa;" class="" data-mce-style="color: #703daa;">T</span><span class="">-></span><span style="color: #703daa;" class="" data-mce-style="color: #703daa;">U</span><span class="">) -> </span><span style="color: #703daa;" class="" data-mce-style="color: #703daa;">Self</span><span class=""><</span><span style="color: #4f8187;" class="" data-mce-style="color: #4f8187;">U</span><span class="">> </span><span style="color: #008400;" class="" data-mce-style="color: #008400;">// error: Cannot specialize non-generic type 'Self'</span></div><div style="font-family: 'menlo'; margin: 0px; line-height: normal;" class="" data-mce-style="font-family: 'menlo'; margin: 0px; line-height: normal;"><span class="">}</span></div><div class="">I *think* I remember reading on here somewhere that the intent is to change map and flatMap to return “Self<U>" pretty much as soon as the language supports it. Someone from Apple would have to confirm that, though… my memory is quite hazy on the matter.</div></div></div></div></blockquote></div><div><span><br data-mce-bogus="1"></span></div><div><span>I'd argue against doing that, because when using map on specialized collections like Set or SortedCollection you usually do NOT want the result to be of the same type because this typically does not make sense, e.g.</span></div><div><span><br data-mce-bogus="1"></span></div><div><span>let employees: Set<Employee> = ...<br data-mce-bogus="1"></span></div><div><span>employees.map { $0.salary } // should NOT be a Set<Salary> dropping equal salaries...<br data-mce-bogus="1"></span></div><div><span><br data-mce-bogus="1"></span></div><div><span>-Thorsten<br data-mce-bogus="1"></span></div></div></body></html>