<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><blockquote type="cite" class=""><div class="">On 09 Feb 2016, at 21:00, <a href="mailto:davesweeris@mac.com" class="">davesweeris@mac.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="">That might solve Inder’s problem, but strictly speaking it doesn’t actually restrict the type to an enum. This struct meets all of RawRepresentable’s requirements:<div class=""><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(39, 139, 210);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> Foo : </span>RawRepresentable<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">typealias</span> RawValue = <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">String</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">init</span>?(rawValue: <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">Foo</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">RawValue</span>) {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">var</span> rawValue: <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">RawValue</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">"bar"</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">}</div><div class=""><br class=""></div><div class="">(Although in practice I can’t think of why that would matter, since you can’t do anything with a RawRepresentable other than get it’s rawValue or call init?(rawValue: String), and neither of those rely on enum features… I’ll stop being pedantic now.)</div></div></div></div></blockquote><br class=""><div>OK, your right, maybe my example was badly chosen. RawRepresentable was the first protocol with an associated type that came into my mind. But the same issue applies for a number of protocols as well, e.g. ArrayLiteralConvertible with associated value Element. Apart from the fact that I don’t like not being able to specify all variable types, you may want to use this in practice to store the values of an Array literal and decide later in what kind of data structure you would like to use to hold the values in the long term.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><br class=""></div><div class="">Anyway, the bigger point is that there’s no way to restrict a generic type to be an enum. I <i class="">think</i> it’s because there wouldn’t be a way to switch on it (or do other enum-ish things) without knowing all its cases, which requires knowing exactly which type it is, which means it’s no longer a generic type. That’s just a guess, though.</div><div class=""><br class=""></div><div class="">Maybe someone should propose that we allow something like this:</div><div class=""><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">func</span> foobar <T, U <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">where</span> T: (<span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">case</span> .foo, <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">case</span> .bar)> (value: <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">T</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">U</span> {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">switch</span> value {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">case</span> .foo: ...</div><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">case</span> .bar: ...</div><div style="margin: 0px; line-height: normal;" class=""> }</div><div style="margin: 0px; line-height: normal;" class="">}</div><div class=""><br class=""></div></div></div></div></div></div></blockquote><div><br class=""></div><div>If you specify all the cases your enum should have, you could just specify the enum as well. The list would have to be exhaustive anyway so that the compiler can check that all cases have been covered in a switch statement and I can’t think of any reason why it would be useful to have two enums with exactly the same cases.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><br class=""></div><div class="">- Dave Sweeris</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 9, 2016, at 10:47, Alex Hoppen 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=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">As for a new syntax, I was also thinking about this issue a few days ago and it occurred to me that there are even more types that cannot be properly represented in Swift. For example there is currently no way (that I know of) to create a variable that can contain any enum that is backed by a String. However, these kinds of constraints can easily be specified in generic constraints (the ones in angle brackets). Maybe we could add the same syntax for variables/constants as well so that the code would look something like the following for your issue:<div class=""><br class=""></div><div class="">var<T, where T: UIViewController, T: MyProtocol> myVar: T</div><div class=""><br class=""></div><div class="">or for enums backed by a String:</div><div class=""><br class=""></div><div class="">var<T: RawRepresentable where T.RawValue == String> myVar: T</div><div class=""><br class=""></div><div class="">Tell me what you think about it.</div><div class=""><br class=""></div><div class="">- Alex<br class=""><div class=""><br class=""></div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 09 Feb 2016, at 15:58, Inder Kumar Rathore . 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 dir="ltr" class="">Hi All,<div class="">I used to do this in Obj-C but I'm unable to do this in swift</div><div class=""><br class=""></div><div class=""><b class=""><i class="">UIViewController<MyProtocol> *delegate;</i></b><br class=""></div><div class=""><b class=""><i class=""><br class=""></i></b></div><div class="">I posted this question on dev forums but didn't get the solution and finally I'm reporting here so that It can be added as a features in the coming releases.</div><div class=""><br class=""></div><div class="">Thanks</div><div class=""><br class=""></div><div class="">Rathore</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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></body></html>