<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br></div><div>On Feb 9, 2016, at 12:16, Alex Hoppen <<a href="mailto:alex@ateamer.de">alex@ateamer.de</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><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></div></div></blockquote><div>Your example was not poorly chosen, I was just pointing out that Swift doesn't currently have a mechanism for explicitly restricting a generic type to *only* being an enum, in the way that you can say "T: class" and then T must be a class (which I think is used so that you know T has reference semantics).</div><div><br></div><blockquote type="cite"><div><div><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="">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></div></div></blockquote><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);">Sorry, I was unclear... I meant T would be restricted to any enum that has *at least* a foo case and a bar case... There could be others, too. </span><span style="background-color: rgba(255, 255, 255, 0);">Speaking of which, there should have been a "default" in there to handle when "value" is one of T's potential other cases.</span></div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);">- Dave Sweeris.</span></div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);"><br>Sent from my iPhone</span></div></body></html>