<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I did some experimenting, and I see that most variance situations <i class="">are</i> covered by the compiler. Generic collections and wrappers, like Optional or Array, are acceptable with .map and .flatMap, but currently benefit from compiler magic to cast those types. However, not all the standard library generic types are convertible because they don’t all carry (the right) .map or .flatMap. Observe:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> GenericType<T></span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">{</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> t: </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">T</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> t: </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>{ </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">t</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = t }</span></div></div><div class=""><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span></p></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> map<U>(by transform: (</span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">U</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">GenericType</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">U</span><span style="font-variant-ligatures: no-common-ligatures" class="">></span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>{ </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">GenericType</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">U</span><span style="font-variant-ligatures: no-common-ligatures" class="">>(transform(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">t</span><span style="font-variant-ligatures: no-common-ligatures" class="">)) }</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> flatMap<U>(by transform: (</span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">U</span><span style="font-variant-ligatures: no-common-ligatures" class="">?) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">GenericType</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">U</span><span style="font-variant-ligatures: no-common-ligatures" class="">>?</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>{ </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> transform(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">t</span><span style="font-variant-ligatures: no-common-ligatures" class="">).</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">map</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">GenericType</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">U</span><span style="font-variant-ligatures: no-common-ligatures" class="">>($0) } }</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// let g = something using `as` and variance (or compiler magic) to cast.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// let G = something without using `as` or variance to cast.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// "Upcasting" the wrapped type: All should return successfully cast types, as an Int is always an Any. Casting with `as` always makes sense here.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> a = </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Optional</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #da2000" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Optional</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// This compiler magic is used absolutely everywhere.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> A = </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Optional</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #da2000" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">).</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">map</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { $0 } </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">as</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Optional</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures" class="">> </span><span style="font-variant-ligatures: no-common-ligatures; color: #929292" class="">// Acceptable.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> b = </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>() </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// This compiler magic sometimes uses NSArray unexpectedly.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> B = </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">>().</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">map</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { $0 } </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">as</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures" class="">> </span><span style="font-variant-ligatures: no-common-ligatures; color: #929292" class="">// Acceptable.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><b class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> c = </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">GenericType</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #da2000" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">GenericType</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// Without compiler magic, requires `as?` and never succeeds.</span></b></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(73, 134, 114);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> C = </span><span style="font-variant-ligatures: no-common-ligatures" class="">GenericType</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #da2000" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">).</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">map</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> { $0 } </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">GenericType</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> </span><span style="font-variant-ligatures: no-common-ligatures; color: #929292" class="">// Acceptable.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// "Downcasting" the wrapped type: Returns `nil` if the cast fails (where the wrapped Any isn't an Int). Casting with `as` usually makes sense.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> x = </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Optional</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #da2000" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Optional</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// Returns Optional<Int>, not Optional<Int>?. This is different from the other two's desired result.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> X = </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Optional</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #da2000" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">).</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">flatMap</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> { $0 </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> } </span><span style="font-variant-ligatures: no-common-ligatures" class="">// Using map would return Optional<Int>?.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> SomeError: </span><span style="font-variant-ligatures: no-common-ligatures; color: #aa2eb8" class="">Error</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { }</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> y = </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures" class="">>([</span><span style="font-variant-ligatures: no-common-ligatures; color: #da2000" class="">4</span><span style="font-variant-ligatures: no-common-ligatures" class="">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #0225bc" class="">""</span><span style="font-variant-ligatures: no-common-ligatures" class="">]) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">as</span><span style="font-variant-ligatures: no-common-ligatures" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">></span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><b class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> Y = </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">try</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>([</span><span style="font-variant-ligatures: no-common-ligatures; color: #da2000" class="">4</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #0225bc" class="">""</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">]).</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">map</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">try</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> ($0 </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) ?? { </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">throw</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">SomeError</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">() }() } </span><span style="font-variant-ligatures: no-common-ligatures" class="">// I don't have any idea how to accomplish that well. flatMap doesn't do the same thing as returning Array<Int>? if one element fails.</span></b></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> z = </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">GenericType</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #da2000" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">GenericType</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// Without compiler magic, always fails.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><b class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> Z = </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">GenericType</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #da2000" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">).</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">flatMap</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> { $0 </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> } </span><span style="font-variant-ligatures: no-common-ligatures" class="">// Does every generic type really need to implement a `map` and `flatMap` for polymorphism to exist?</span></b></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// * Ranges——a standard library type with some serious problems. * //</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(170, 46, 184);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">class</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> Number: </span><span style="font-variant-ligatures: no-common-ligatures" class="">Comparable</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">{</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">// Stores a double. Implementation is hidden.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">class</span><span style="font-variant-ligatures: no-common-ligatures" class=""> RationalNumber: </span><span style="font-variant-ligatures: no-common-ligatures; color: #5faeae" class="">Number</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>{ </span><span style="font-variant-ligatures: no-common-ligatures" class="">/* Extra functionality */</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> }</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> rationalRange: </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Range</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #5faeae" class="">RationalNumber</span><span style="font-variant-ligatures: no-common-ligatures" class="">> = .</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #da2000" class="">1.0</span><span style="font-variant-ligatures: no-common-ligatures" class="">) ..< .</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #da2000" class="">2.0</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><b class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> numberRange: </span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Range</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #5faeae" class="">Number</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">rationalRange</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// There's no .map to fix this problem!</span></b></div></div></blockquote><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span></div></div><div class="">Obviously, there is some room for improvement in polymorphism among generic types. Upcasting, I think, is always a trivial matter and could be supported. Downcasting is less cut and dry, but would still make sense if we could improve the situation we have. Having to use .map and .flatMap every time you want to polymorphize any generic type—besides the compiler’s babies—is a pain. What do you guys think?</div><div class=""><br class=""></div><div class="">Whether or not we find some sort of improvement here, I still wonder how I could identify my generic type from a parent type:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> any: </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">GenericType</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #5b2699" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">></span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">if</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> generic = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">any</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #498672" class="">GenericType</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #008f01" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// Currently fails for any GenericType<Int>, GenericType<String>, etc.</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>{ </span><span style="font-variant-ligatures: no-common-ligatures" class="">/* Do something with my generic type */</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> }</span></div></div></blockquote><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 10, 2016, at 9:18 AM, David Waite <<a href="mailto:david@alkaline-solutions.com" class="">david@alkaline-solutions.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I wouldn’t keep it that narrow - monadic types like Optional also benefit from variance:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">func p(_ data:Any?) { </span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> if data != nil { </span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> data.map { print($0) }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> } </span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(169, 169, 169); background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">} </span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">var a:String? = "foo"</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(51, 187, 200); background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">p(a)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// -> “foo"</span></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">-DW</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 9, 2016, at 12:24 PM, Hooman Mehr via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">For the specific case of custom collections, I think it is worth providing a protocol as Doug noted before.<div class=""><br class=""></div><div class="">Quoting Doug Gregor (1/13/16, thread: "<font color="rgba(0, 0, 0, 0.85098)" face="Helvetica Neue" class="">Make generics covariant and add generics to protocols”)</font>: </div><div class=""><blockquote type="cite" class="">Swift’s value-semantic collections are covariant in their generic parameters, which we do through some fairly tight coupling between the compiler and standard library. From a theoretical standpoint, I’m very happy with the way value-semantic collections provide subtyping and mutation while maintaining soundness (== no runtime checks needed), and for me <b class=""><i class="">I would consider it “enough” if we were to formalize that compiler/collection type interaction with some kind of protocol so other collection types could opt in to subtyping</i></b>, because I don’t think variance—as a language feature—carries its weight outside of the fairly narrow collection-subtyping cases.<br class=""></blockquote><div class="">(Emphasis mine) </div><div class=""><br class=""></div><div class="">I also agree with Doug and you that variance does not carry its weight outside of collection-subtyping cases.</div></div></div></div></div></div></blockquote></div><br class=""></div></div><div class="">-DW</div></div></div></blockquote></div><br class=""></body></html>