<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="">To my understanding, the following is exactly as it should be:<div class=""><br class=""></div><div class="">FooStruct&lt;String&gt; as? FooStruct&lt;Any&gt; // Compiles but conversion fails, becomes nil, and that's normal</div><div class=""><br class=""></div><div class="">The reason for this is that FooStruct&lt;String&gt; is not a subtype of FooStruct&lt;Any&gt; (or just FooStruct), while String is of course a subtype of Any, because generic types are not covariant in Swift, and that's the way it should be for a sound static type system. My comments on this were related to what you wrote about &nbsp;arrays.</div><div class=""><br class=""></div><div class="">In theory a protocol without associated types could be synthesized for all the non generic properties and methods of a generic type, with the ability of casting to it and possibly from it.</div><div class=""><br class=""></div><div class="">It's a useful idea, and I'm all for it (I think literally everyone that uses generics and protocols with associated types encountered these kinds of problems at least once), I'm just saying that I'd rather work on generalized existentials which have already been considered by the core team, at least from a theoretical standpoint, have a greater scope and include cases like this. In general I tend to prefer broader, more generic solutions rooted in type theory when dealing with generics and protocols, but that's just me.&nbsp;</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Elviro</div><div class=""><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">Il giorno 08 ago 2017, alle ore 10:44, Logan Shire via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; ha scritto:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Thanks for the feedback!<div class=""><br class=""></div><div class="">Félix, sorry about the confusion between FooStruct and FooProtocol - I'll refer to them as such moving forwards.<br class=""></div><div class=""><br class=""></div><div class="">David, I don't believe you should be able to cast an [FooStruct&lt;String&gt;] to an [FooStruct&lt;Any&gt;] because those are both valid specifications. If&nbsp;<a href="https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#generalized-existentials" class="">Generalized Existentials</a>&nbsp;are implemented, that would be another story, but that's outside the scope of this proposal. I do believe you should be able to cast [FooStruct&lt;String&gt;] to [FooStruct], and that you should be able to flatMap [FooStruct] into [FooStruct&lt;Any&gt;] with as?, but all of the casts would fail and you would be left with an empty array.</div><div class=""><br class=""></div><div class="">In regards to the Named protocol, yes, that is the current idiomatic approach to solving this problem (along with making a function unnecessarily generic and then using the generic type as a constraint). I want to avoid jumping through those hoops. We'd essentially be synthesizing the Named protocol with the same name as the non-generic version of the type. I.e. FooStruct&lt;T&gt;: FooStruct</div><div class=""><br class=""></div><div class="">Félix, I believe the above answers some of your questions, but in regards to protocols with associated types, I'd imagine it would work the same way. If FooProtocol has an associated type T, there would be another protocol, FooProtocol, without the associated type. (behind the scenes its garbled name would be different)</div><div class=""><br class=""></div><div class="">Also, an aside, it would be nice if protocols could use the generic syntax for their associated type constraints. I.e. "FooProtocol with T = Int" could be expressed as FooProtocol&lt;T: Int&gt;. It feels strange that we have two different syntaxes for essentially the same language construct. At the very least, I want some way to cast a value to a protocol type with an associated value. E.g. "if let grassEater = any as? Animal where Food = Grass"</div><div class=""><br class=""></div><div class="">Elviro, yes, the generalized existentials would help a lot here, but that's outside the scope of what I'm proposing. In the near term I'd like to be able to use a generic type's non-generic interface, casting to and from it. See the above discussion regarding the Named protocol. Essentially we'd be synthesizing the Named protocol, but where the type's name is the same as the non-generic version of the type name.</div><div class=""><br class=""></div><div class="">FooStruct&lt;String&gt; as FooStruct // works</div><div class="">FooStruct as? FooStruct&lt;String&gt; // works</div><div class="">FooStruct as? FooStruct&lt;Any&gt; // Compiles but conversion fails, becomes nil</div><div class="">FooStruct&lt;String&gt; as? FooStruct&lt;Any&gt; // Compiles but conversion fails, becomes nil</div><div class=""><div class=""><br class=""></div><div class="">Let me know if you have any other questions!</div><div class=""><br class=""></div><div class="">Logan<br class=""><br class=""><div class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Tue, Aug 8, 2017 at 9:43 AM Félix Cloutier &lt;<a href="mailto:felixcloutier@icloud.com" target="_blank" class="">felixcloutier@icloud.com</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">I'm going to separate your examples into FooStruct and FooProtocol for clarity.</div><div class=""><br class=""></div><div class="">I agree that generics tend to propagate virally and I remember that at some point I wanted type erasure, though I don't remember for what exactly. The solution for `sayHi`, right now, is to make that one generic too:</div><div class=""><br class=""></div><div class=""></div><blockquote type="cite" class="">func&nbsp;sayHi&lt;T&gt;(to foo: T) where T: FooProtocol {<br class="">&nbsp; &nbsp;&nbsp;print("hi&nbsp;\(<a href="http://foo.name/" target="_blank" class="">foo.name</a>)")<br class="">}</blockquote><br class=""><div class="">The "let&nbsp;foos: [FooStruct] = [FooStruct(name:&nbsp;"Int", value:&nbsp;2),&nbsp;FooStruct(name:&nbsp;"Double", value:&nbsp;2.0)]" part can't work for structs because arrays require each element to have the same size (but it could work for classes).</div><div class=""><br class=""></div><div class="">Even then, you couldn't infer the type to [FooClass&lt;Any&gt;] because contravariance isn't permissible in that situation: doing so would allow you to assign any Any to a FooClass's value.</div><div class=""><br class=""></div><div class="">Another problem that this would have to solve is that once you lose the associatedtype that came with the protocol, there is nothing you can do to recover it; you currently can't express "FooProtocol with T = Int" as a type that you can cast to, so you would only be able to pass the instance to functions that don't have constraints on T.</div><div class=""><br class=""></div><div class="">But all in all, with my current understanding of the issue, I think that I'm favorable to the idea.</div><div class=""><br class=""></div><div class="">Félix</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""></blockquote></div></div><div style="word-wrap:break-word" class=""><div class=""><blockquote type="cite" class=""><div class="">Le 7 août 2017 à 19:35, David Sweeris via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; a écrit :</div><br class="m_-7626455572048145649m_-643534145729421538Apple-interchange-newline"></blockquote></div></div><div style="word-wrap:break-word" class=""><div class=""><blockquote type="cite" class=""><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" class=""><blockquote type="cite" class=""><div class=""><br class="m_-7626455572048145649m_-643534145729421538Apple-interchange-newline">On Aug 7, 2017, at 3:00 PM, Logan Shire via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_-7626455572048145649m_-643534145729421538Apple-interchange-newline"><div class=""><div style="word-wrap:break-word" class="">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 style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class=""><span style="color:rgb(194,52,155)" class="">struct</span>&nbsp;Foo&lt;T&gt; {</div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class="">&nbsp; &nbsp;&nbsp;<span style="color:rgb(194,52,155)" class="">let</span>&nbsp;name:&nbsp;<span style="color:rgb(0,175,202)" class="">String</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class="">&nbsp; &nbsp;&nbsp;<span style="color:rgb(194,52,155)" class="">let</span>&nbsp;value:&nbsp;<span style="color:rgb(147,201,106)" class="">T</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class="">}</div></div><div class=""><br class=""></div><div class="">or:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class=""><span style="color:rgb(194,52,155)" class="">protocol</span>&nbsp;Foo {</div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class="">&nbsp; &nbsp;&nbsp;<span style="color:rgb(194,52,155)" class="">associatedtype&nbsp;</span><span style="color:rgb(147,201,106)" class="">T</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class="">&nbsp; &nbsp;&nbsp;<span style="color:rgb(194,52,155)" class="">var</span>&nbsp;name:&nbsp;<span style="color:rgb(0,175,202)" class="">String&nbsp;</span>{&nbsp;<span style="color:rgb(194,52,155)" class="">get&nbsp;</span>}</div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class="">&nbsp; &nbsp;&nbsp;<span style="color:rgb(194,52,155)" class="">var</span>&nbsp;value:&nbsp;<span style="color:rgb(147,201,106)" class="">T&nbsp;</span>{&nbsp;<span style="color:rgb(194,52,155)" class="">get&nbsp;</span>}</div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class="">}</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 style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class=""><span style="color:rgb(194,52,155)" class="">func</span>&nbsp;sayHi(to foo:&nbsp;<span style="text-decoration:underline;color:rgb(0,175,202)" class="">F</span><span style="color:rgb(0,175,202)" class="">oo</span>) {</div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class="">&nbsp; &nbsp;&nbsp;<span style="color:rgb(0,175,202)" class="">print</span>(<span style="color:rgb(228,67,71)" class="">"hi&nbsp;</span>\<span style="color:rgb(228,67,71)" class="">(</span><a href="http://foo.name/" target="_blank" class="">foo.name</a><span style="color:rgb(228,67,71)" class="">)"</span>)</div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class="">}</div></div><div class=""><br class=""></div><div class="">But instead I get the error, “Reference to generic type Foo requires arguments in &lt;…&gt;”</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 style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class=""><span style="color:rgb(194,52,155)" class="">let</span>&nbsp;foos: [Foo] = [<span style="color:rgb(147,201,106)" class="">Foo</span>(name:&nbsp;<span style="color:rgb(228,67,71)" class="">"Int"</span>, value:&nbsp;<span style="color:rgb(139,132,207)" class="">2</span>),&nbsp;<span style="color:rgb(147,201,106)" class="">Foo</span>(name:&nbsp;<span style="color:rgb(228,67,71)" class="">"Double"</span>, value:&nbsp;<span style="color:rgb(139,132,207)" class="">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 style="font-family:Menlo;background-color:rgb(40,43,53);color:rgb(194,52,155)" class="">let</span><span style="color:rgb(255,255,255);font-family:Menlo;background-color:rgb(40,43,53)" class="">&nbsp;foos = [</span><span style="font-family:Menlo;background-color:rgb(40,43,53);color:rgb(147,201,106)" class="">Foo</span><span style="color:rgb(255,255,255);font-family:Menlo;background-color:rgb(40,43,53)" class="">(name:&nbsp;</span><span style="font-family:Menlo;background-color:rgb(40,43,53);color:rgb(228,67,71)" class="">"Int"</span><span style="color:rgb(255,255,255);font-family:Menlo;background-color:rgb(40,43,53)" class="">, value:&nbsp;</span><span style="font-family:Menlo;background-color:rgb(40,43,53);color:rgb(139,132,207)" class="">2</span><span style="color:rgb(255,255,255);font-family:Menlo;background-color:rgb(40,43,53)" class="">),&nbsp;</span><span style="font-family:Menlo;background-color:rgb(40,43,53);color:rgb(147,201,106)" class="">Foo</span><span style="color:rgb(255,255,255);font-family:Menlo;background-color:rgb(40,43,53)" class="">(name:&nbsp;</span><span style="font-family:Menlo;background-color:rgb(40,43,53);color:rgb(228,67,71)" class="">"Double"</span><span style="color:rgb(255,255,255);font-family:Menlo;background-color:rgb(40,43,53)" class="">, value:&nbsp;</span><span style="font-family:Menlo;background-color:rgb(40,43,53);color:rgb(139,132,207)" class="">2.0</span><span style="color:rgb(255,255,255);font-family:Menlo;background-color:rgb(40,43,53)" class="">)]</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&lt;Any&gt;] = ..."?&nbsp;</div><div class=""><br class=""></div><div class=""><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap:break-word" class=""><div class="">I’d like to propose being able to use the non-generic interface of a type normally.&nbsp;</div><div class="">I.e. if you have a type Foo&lt;T&gt;, it is implicitly of type Foo as well. The type Foo could be used like any other type.</div><div class="">It could be a parameter in a function, a variable, or even the generic type of another type (like a Dictionary&lt;String, Foo&gt;)</div><div class=""><br class=""></div><div class="">The only restriction is that if you want to call or access, directly or indirectly, a function or member that requires the generic type,</div><div class="">the generic type would have to be known at that point.</div><div class=""><br class=""></div><div class="">Foo&lt;T&gt; should be able to be implicitly casted to Foo wherever you want, and Foo could be cast to Foo&lt;T&gt; conditionally.</div><div class="">Initializers would still obviously have to know the generic type, but given the above example, you should be able to:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class=""><span style="color:rgb(194,52,155)" class="">let</span>&nbsp;names =&nbsp;<span style="color:rgb(147,201,106)" class="">foos</span>.<span style="color:rgb(0,175,202)" class="">map</span>&nbsp;{ $0.<span style="color:rgb(147,201,106)" class="">name</span>&nbsp;}</div></div><div class=""><br class=""></div><div class="">However, you could not do the following:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)" class=""><span style="color:rgb(194,52,155)" class="">let</span><span class="m_-7626455572048145649m_-643534145729421538Apple-converted-space">&nbsp;</span>foos = [<span style="color:rgb(147,201,106)" class="">Foo</span>]()</div></div><div class=""><br class=""></div><div class="">Because the initializer would need to know the generic type in order to allocate the memory.</div><div class=""><br class=""></div><div class="">Let me know what you think!</div></div></div></blockquote></div><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" class=""><br class=""></div><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" class="">The idiomatic solution would be to create a `Named` protocol with a `var name: String {get}` property, and write your function like `func sayHi(to foo:Named) {...}`. However, this `Named`protocol is really pretty trivial -- its purpose is simply to "degenericify" a generic type, not to provide any semantic meaning. Perhaps an analogy could be drawn between such "trivial protocols" and how we sometimes view tuples as "trivial structs"? Dunno, maybe I'm just trying to turn two trees into a forest, but this kinda smells like it might be part of a bigger issue, and if it is I'd rather tackle that and then see if we still need to address anything here.</div><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" class=""><br class=""></div><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" class="">+1, either way, though.</div><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" class=""><br class=""></div><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" class="">- Dave Sweeris</div></div></blockquote></div></div><div style="word-wrap:break-word" class=""><div class=""><blockquote type="cite" class=""><div class=""><span 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;float:none;display:inline!important" class="">_______________________________________________</span><br 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" class=""><span 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;float:none;display:inline!important" class="">swift-evolution mailing list</span><br 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" class=""><a href="mailto:swift-evolution@swift.org" 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" target="_blank" class="">swift-evolution@swift.org</a><br 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" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" 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" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></div></blockquote></div></div></div></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></div></body></html>