<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br></div><div>On Aug 8, 2017, at 06:38, Karl Wagner <<a href="mailto:razielim@gmail.com">razielim@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><br class=""><div><blockquote type="cite" class=""><div class="">On 8. Aug 2017, at 04:35, David Sweeris 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=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On Aug 7, 2017, at 3:00 PM, Logan Shire 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=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">One of my longstanding frustrations with generic types and protocols has been how hard it is to work with them when their type is unspecified.<div class="">Often I find myself wishing that I could write a function that takes a generic type or protocol as a parameter, but doesn’t care what its generic type is.</div><div class=""><br class=""></div><div class="">For example, if I have a type:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);"><span class="" style="color: rgb(194, 52, 155);">struct</span> Foo<T> {</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);"> <span class="" style="color: rgb(194, 52, 155);">let</span> name: <span class="" style="color: rgb(0, 175, 202);">String</span></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);"> <span class="" style="color: rgb(194, 52, 155);">let</span> value: <span class="" style="color: rgb(147, 201, 106);">T</span></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);">}</div></div><div class=""><br class=""></div><div class="">or:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);"><span class="" style="color: rgb(194, 52, 155);">protocol</span> Foo {</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);"> <span class="" style="color: rgb(194, 52, 155);">associatedtype </span><span class="" style="color: rgb(147, 201, 106);">T</span></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);"> <span class="" style="color: rgb(194, 52, 155);">var</span> name: <span class="" style="color: rgb(0, 175, 202);">String </span>{ <span class="" style="color: rgb(194, 52, 155);">get </span>}</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);"> <span class="" style="color: rgb(194, 52, 155);">var</span> value: <span class="" style="color: rgb(147, 201, 106);">T </span>{ <span class="" style="color: rgb(194, 52, 155);">get </span>}</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);">}</div></div><div class=""><br class=""></div><div class="">And I want to write a function that only cares about Foo.name, I’d like to be able to:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);"><span class="" style="color: rgb(194, 52, 155);">func</span> sayHi(to foo: <span class="" style="text-decoration: underline; color: rgb(0, 175, 202);">F</span><span class="" style="color: rgb(0, 175, 202);">oo</span>) {</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);"> <span class="" style="color: rgb(0, 175, 202);">print</span>(<span class="" style="color: rgb(228, 67, 71);">"hi </span>\<span class="" style="color: rgb(228, 67, 71);">(</span>foo.name<span class="" style="color: rgb(228, 67, 71);">)"</span>)</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);">}</div></div><div class=""><br class=""></div><div class="">But instead I get the error, “Reference to generic type Foo requires arguments in <…>”</div><div class=""><br class=""></div><div class="">Also, when you want to have a polymorphic array of generic types, you can’t:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255); background-color: rgb(40, 43, 53);"><span class="" style="color: rgb(194, 52, 155);">let</span> foos: [Foo] = [<span class="" style="color: rgb(147, 201, 106);">Foo</span>(name: <span class="" style="color: rgb(228, 67, 71);">"Int"</span>, value: <span class="" style="color: rgb(139, 132, 207);">2</span>), <span class="" style="color: rgb(147, 201, 106);">Foo</span>(name: <span class="" style="color: rgb(228, 67, 71);">"Double"</span>, value: <span class="" style="color: rgb(139, 132, 207);">2.0</span>)]</div></div><div class=""><br class=""></div><div class="">And if you remove the explicit type coercion, you just get [Any]</div><div class=""><br class=""></div><div class=""><span class="" style="font-family: Menlo; background-color: rgb(40, 43, 53); color: rgb(194, 52, 155);">let</span><span class="" style="color: rgb(255, 255, 255); font-family: Menlo; background-color: rgb(40, 43, 53);"> foos = [</span><span class="" style="font-family: Menlo; background-color: rgb(40, 43, 53); color: rgb(147, 201, 106);">Foo</span><span class="" style="color: rgb(255, 255, 255); font-family: Menlo; background-color: rgb(40, 43, 53);">(name: </span><span class="" style="font-family: Menlo; background-color: rgb(40, 43, 53); color: rgb(228, 67, 71);">"Int"</span><span class="" style="color: rgb(255, 255, 255); font-family: Menlo; background-color: rgb(40, 43, 53);">, value: </span><span class="" style="font-family: Menlo; background-color: rgb(40, 43, 53); color: rgb(139, 132, 207);">2</span><span class="" style="color: rgb(255, 255, 255); font-family: Menlo; background-color: rgb(40, 43, 53);">), </span><span class="" style="font-family: Menlo; background-color: rgb(40, 43, 53); color: rgb(147, 201, 106);">Foo</span><span class="" style="color: rgb(255, 255, 255); font-family: Menlo; background-color: rgb(40, 43, 53);">(name: </span><span class="" style="font-family: Menlo; background-color: rgb(40, 43, 53); color: rgb(228, 67, 71);">"Double"</span><span class="" style="color: rgb(255, 255, 255); font-family: Menlo; background-color: rgb(40, 43, 53);">, value: </span><span class="" style="font-family: Menlo; background-color: rgb(40, 43, 53); color: rgb(139, 132, 207);">2.0</span><span class="" style="color: rgb(255, 255, 255); font-family: Menlo; background-color: rgb(40, 43, 53);">)]</span></div><div class=""><br class=""></div><div class="">I wish that could be inferred to be [Foo].</div></div></div></blockquote><div class=""><br class=""></div><div class="">What happens if you try to say "foos: [Foo<Any>] = ..."? </div><div class=""><br class=""></div></div></div></blockquote><div><br class=""></div><div>Foo<Int> and Foo<Any> are very different. Otherwise, you could take a Foo<Int>, cast it to a Foo<Any> and set a String as its value.</div><div><br class=""></div><div>I think what he means are partial generics, e.g: Foo<_>.</div></div></div></blockquote><br><div><span style="background-color: rgba(255, 255, 255, 0);">Oh I know, I just couldn't remember if it'd work as long as you didn't mess with the generic bits.</span><br></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">- Dave Sweeris</span></div></body></html>