<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"><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="">Not insolence at all. :-) It's important to justify or come up with good logic here, and doubly so on Linux where NS_ENUM/NS_OPTIONS aren't standard.</div><div class=""><br class=""></div><div class="">The usefulness of an enum is that you can guarantee that all cases are enumerated. But even that's not always true; people <i class="">do</i>&nbsp;add new elements to C enums without breaking binary compatibility. Or they have "private" cases that are ABI-compatible but do not correspond to a public declaration. So we'll <i class="">already</i>&nbsp;probably be dropping the idea of exhaustive switch over an imported enum unless we have some prior knowledge. (That still needs design.)</div><div class=""><br class=""></div><div class="">So then what's the cost of using an enum over a struct for the option set? I guess it's mostly the <i class="">implication</i>&nbsp;that you could switch over it, something you should never do with an option set. There are also option sets with predefined combinations of options:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">typedef NS_OPTIONS(NSUInteger, MyOptions) {</div><div class="">&nbsp; MyOptionA = 1,</div><div class="">&nbsp; MyOptionB = 2,</div><div class="">&nbsp; MyOptionC = 4,</div><div class="">&nbsp; MyOptionDefault = MyOptionA | MyOptionC,</div><div class="">};</div></blockquote><div class=""><br class=""></div><div class="">…which would be weird for an enum. As you say, though, it's not a deal-breaker.</div><div class=""><br class=""></div><div class="">For your&nbsp;<a href="https://gist.github.com/anonymous/232d62d77999f5eb27cd" class="">gist</a>&nbsp; I'm not really sure what separates type 1 from type 2. Is it the explicit values assigned to the cases? The fact that all of the "type 2" values are powers of 2? And what makes type 3 a set of constants rather than just an enum or option set with a compatibility alias for its first element?</div><div class=""><br class=""></div><div class="">I don't think we want to make these heuristics much more complicated—the more rules there are, the more likely people will be confused when they don't work. One advantage of the current heuristic is that it encourages people to make their headers better; a disadvantage is that not all headers are editable. Maybe we can fix <i class="">that</i>&nbsp;problem instead, for example by looking for annotations on redeclarations of the enum.</div><div class=""><br class=""></div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 11, 2016, at 13:25 , Ryan Lovelett &lt;<a href="mailto:swift-dev@ryan.lovelett.me" class="">swift-dev@ryan.lovelett.me</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">


<title class=""></title>

<div class=""><div class="">I think I get where you are going. But I'm not going to drop it quite that easily. Forgive my insolence but I still think there is room for a sane default here.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">To answer your question: it is an enum that deliberately does not start at 0. If Swift made that the default import symantics then I think all the cases you covered (selfishly even my case) would be adequately addressable.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">Lets say there are, at least, 3 types of enums:<br class=""></div>
<ol class=""><li class="">A non-overlapping, non-option set enum<br class=""></li><li class="">A non-overlapping option set</li><li class="">An overlapping option set<br class=""></li></ol><div class="">&nbsp;</div>
<div class="">In this case, I'd say the enum was of type 2; a non-overlapping option set. If it was something the developer needed to conform to the `OptionSetType` then the developer who imported the type could provide a simple extension to the enum that provides conformance with the `OptionSetType`. Voila. Now it is an option set.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">More concretely:<br class=""></div>
<div class="">&nbsp;</div>
<div class="">&nbsp; &nbsp; enum Foo: Int {<br class=""></div>
<div class="">&nbsp; &nbsp; &nbsp; case A = 1<br class=""></div>
<div class="">&nbsp; &nbsp; &nbsp; case B = 2<br class=""></div>
<div class="">&nbsp; &nbsp; }<br class=""></div>
<div class="">&nbsp;</div>
<div class="">&nbsp; &nbsp; // Not automatic but available in this hypothetical world<br class=""></div>
<div class="">&nbsp; &nbsp; extension Foo: OptionSetType {<br class=""></div>
<div class="">&nbsp; &nbsp; &nbsp; init(rawValue: Int) {<br class=""></div>
<div class="">&nbsp; &nbsp; &nbsp; &nbsp; self = Foo.init(rawValue: rawValue) ?? Foo.A<br class=""></div>
<div class="">&nbsp; &nbsp; &nbsp; }<br class=""></div>
<div class="">&nbsp; &nbsp; }<br class=""></div>
<div class="">&nbsp;</div>
<div class="">If it was really some sort of constant then now it can be accessed via it's `rawValue` property (isn't that the way it would be accessed as it currently stands?).<br class=""></div>
<div class="">&nbsp;</div>
<div class="">If it was just an enum, well then that's covered too.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">I tried compiling a few contrived enums based on this one to illustrate how I'd expect them to be mapped. <a href="https://gist.github.com/anonymous/232d62d77999f5eb27cd" class="">I've put them into this gist</a>.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">I think if you took it in terms of pattern matching where the the first case is the strictest pattern (and thus the hardest to conform to) by the time you reach the 3rd case your back to the current behavior. Swift would then be able to account for more generic enum imports than it currently can.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">On Mon, Jan 11, 2016, at 02:51 PM, Jordan Rose wrote:<br class=""></div>
<blockquote type="cite" class=""><div class="">Oh, you can certainly define a macro named NS_ENUM yourself, but that doesn't really seem like the right thing to do on Linux.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">As for why we can't "guess" that this is a true enum, consider the following:<br class=""></div>
<div class="">&nbsp;</div>
<blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-top-width:initial;border-right-width:initial;border-bottom-width:initial;border-left-width:initial;border-top-color:initial;border-right-color:initial;border-bottom-color:initial;border-left-color:initial;border-image-source:initial;border-image-slice:initial;border-image-width:initial;border-image-outset:initial;border-image-repeat:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;" class=""><div class="">enum Foo {<br class=""></div>
<div class="">&nbsp; A = 1,<br class=""></div>
<div class="">&nbsp; B = 2,<br class=""></div>
<div class="">};<br class=""></div>
</blockquote><div class="">&nbsp;</div>
<div class="">Is this a very small option set, or an enum that deliberately doesn't start at 0? Or just a way to define constants for some other type, rather than using "static const"? If the compiler guesses here, (a) it might guess wrong, making the type hard to use <i class="">now,</i>&nbsp;and (b) if the headers are updated in a newer version of the library, it might <i class="">change</i>&nbsp;its guess, which would break source compatibility. (This can happen anyway, e.g. the first time an annotation is added, but at least that's supposed to be changing in the right direction, and is unlikely to change again.)<br class=""></div>
<div class="">&nbsp;</div>
<div class="">Hope that clarifies the motivation here, even if it's less than satisfactory.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">Jordan<br class=""></div>
<div class="">&nbsp;</div>
<div class="">&nbsp;</div>
<div class=""><blockquote type="cite" class=""><div class="">On Jan 11, 2016, at 11:22, Ryan Lovelett &lt;<a href="mailto:swift-dev@ryan.lovelett.me" class="">swift-dev@ryan.lovelett.me</a>&gt; wrote:<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><div class=""><div class="">Jordan,<br class=""></div>
<div class="">&nbsp;</div>
<div class="">Perhaps I'm not following that parenthetical comment. Are you saying that even if I could add <span class="highlight" style="background-color: rgb(255, 255, 255)"><span style="" class=""><span class="font" style="font-family:Menlo, monospace"><span class="size" style="font-size:12px">NS_ENUM&nbsp;</span></span></span></span>to the header it still wouldn't compile into a true Swift enum (on a non-Apple platform)?<br class=""></div>
<div class="">&nbsp;</div>
<div class="">Swift's pattern matching capability is probably a top 3 reason why I am trying to port my C application to use Swift rather than just straight C. Not being able to get this nicety out-of-the-box is just part of doing business on a bleeding edge programming language.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">However this&nbsp;<i class="">seems</i> like something that should be able to be achieved. Looking at the fe_type enum definition, because it had no bit pattern associated with it, I would have assumed it was a true non-overlapping enum. Its probable I'm not seeing the whole landscape here so would you be able to illuminate why isn't this the default?<br class=""></div>
<div class="">&nbsp;</div>
<div class="">On Mon, Jan 11, 2016, at 01:11 PM, Jordan Rose wrote:<br class=""></div>
<blockquote type="cite" class=""><div class="">Right. This is because Swift can't tell if your enum is actually an option set, a true, non-overlapping enum, or just a set of related constants, so it picks the lowest common denominator. We currently don't have a great way to override that in headers you don't control.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">(Heck, on non-Apple platforms we don't have a great way to do it in headers you do control; Swift is currently keying off the macro names.)<br class=""></div>
<div class="">&nbsp;</div>
<div class="">Jordan<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><blockquote type="cite" class=""><div class="">On Jan 10, 2016, at 13:25 , Austin Zheng via swift-dev &lt;<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>&gt; wrote:<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><div class=""><span class="font" style="font-family:Helvetica"><span class="size" style="font-size:12px">I spoke too soon, the cases are also defined as values of that type. So, a working version of your code:</span></span><br class=""></div>
<div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;orphans:auto;text-align:start;text-indent:0px;text-transform:none;white-space:normal;widows:auto;word-spacing:0px;-webkit-text-stroke-width:0px;" class="">&nbsp;</div>
<div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;orphans:auto;text-align:start;text-indent:0px;text-transform:none;white-space:normal;widows:auto;word-spacing:0px;-webkit-text-stroke-width:0px;" class=""><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(112, 61, 170);" class=""><span class="colour" style="color:rgb(187, 44, 162)">extension</span><span style="" class=""><span class=""></span></span><span class="colour" style="color:rgb(79, 129, 135)">fe_type</span><span style="" class=""><span class=""></span>:<span class=""></span></span>CustomStringConvertible<span style="" class=""><span class=""></span>{</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:14px;line-height:normal;font-family:Menlo;" class=""><span class=""></span><span class="colour" style="color:rgb(187, 44, 162)">public</span><span class=""></span><span class="colour" style="color:rgb(187, 44, 162)">var</span><span class=""></span>description:<span class=""></span><span class="colour" style="color:rgb(112, 61, 170)">String</span><span class=""></span>{<br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(187, 44, 162);" class=""><span style="" class=""><span class=""></span></span>switch<span style="" class=""><span class=""></span></span>self<span style="" class=""><span class=""></span>{</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(187, 44, 162);" class=""><span style="" class=""><span class=""></span></span>case<span style="" class=""><span class=""></span></span><span class="colour" style="color:rgb(79, 129, 135)">FE_QPSK</span><span style="" class="">:<span class=""></span></span>return<span style="" class=""><span class=""></span></span><span class="colour" style="color:rgb(209, 47, 27)">"QPSK"</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(187, 44, 162);" class=""><span style="" class=""><span class=""></span></span>case<span style="" class=""><span class=""></span></span><span class="colour" style="color:rgb(79, 129, 135)">FE_QAM</span><span style="" class="">:<span class=""></span></span>return<span style="" class=""><span class=""></span></span><span class="colour" style="color:rgb(209, 47, 27)">"QAM"</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(187, 44, 162);" class=""><span style="" class=""><span class=""></span></span>case<span style="" class=""><span class=""></span></span><span class="colour" style="color:rgb(79, 129, 135)">FE_OFDM</span><span style="" class="">:<span class=""></span></span>return<span style="" class=""><span class=""></span></span><span class="colour" style="color:rgb(209, 47, 27)">"OFDM"</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(187, 44, 162);" class=""><span style="" class=""><span class=""></span></span>case<span style="" class=""><span class=""></span></span><span class="colour" style="color:rgb(79, 129, 135)">FE_ATSC</span><span style="" class="">:<span class=""></span></span>return<span style="" class=""><span class=""></span></span><span class="colour" style="color:rgb(209, 47, 27)">"ATSC"</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(209, 47, 27);" class=""><span style="" class=""><span class=""></span></span><span class="colour" style="color:rgb(187, 44, 162)">default</span><span style="" class="">:<span class=""></span></span><span class="colour" style="color:rgb(61, 29, 129)">fatalError</span><span style="" class="">(</span>"can't be exhaustive"<span style="" class="">)</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:14px;line-height:normal;font-family:Menlo;" class="">&nbsp; &nbsp; }<br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:14px;line-height:normal;font-family:Menlo;" class="">&nbsp; }<br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:14px;line-height:normal;font-family:Menlo;" class="">}<br class=""></div>
<div class="">&nbsp;</div>
<div class="">Best,<br class=""></div>
<div class="">Austin<br class=""></div>
<div class=""><div class="">&nbsp;</div>
<div class=""><blockquote type="cite" class=""><div class="">On Jan 10, 2016, at 1:22 PM, Austin Zheng &lt;<a href="mailto:austinzheng@gmail.com" class="">austinzheng@gmail.com</a>&gt; wrote:<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><div style="word-wrap:break-word;-webkit-line-break:after-white-space;" class=""><div class="">Hi Ryan,<br class=""></div>
<div class="">&nbsp;</div>
<div class="">Apologies, I should have been more clear. In Xcode you can alt(?)-click on a type (e.g the 'MyType' in "let a : MyType = 123") in the IDE to pop up a little window that shows you the definition, including the type and some other information. If you're on a Linux box or not using an IDE you probably don't have that option.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">The only methods I see exposed on the Swift imported type are initializers taking a integer raw value, and a 'rawValue' property for getting back out the raw value. Hope that helps.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">Austin<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><blockquote type="cite" class=""><div class="">On Jan 10, 2016, at 1:18 PM, Ryan Lovelett &lt;<a href="mailto:swift-dev@ryan.lovelett.me" class="">swift-dev@ryan.lovelett.me</a>&gt; wrote:<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><div class=""><div class="">Austin,<br class=""></div>
<div class="">&nbsp;</div>
<div class="">I guess I should say that the `typedef` is coming from a<span class=""></span><a href="http://lxr.free-electrons.com/source/include/linux/dvb/frontend.h?v=3.2" class="">Linux kernel header</a>. So I don't think I'm going to be able to add any macros to the definition.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">What do you mean about alt-click? Alt click where?<br class=""></div>
<div class="">&nbsp;</div>
<div class="">On Sun, Jan 10, 2016, at 04:12 PM, Austin Zheng wrote:<br class=""></div>
<blockquote type="cite" class=""><div class="">fe_type is being imported as a struct (alt-click 'fe_type' in Swift). I think if you want it to be imported as an enum you need to use the NS_ENUM macro in the definition, which might not be possible in your case.<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><div class="">Austin<br class=""></div>
<div class=""><div class="">&nbsp;</div>
<div class=""><blockquote type="cite" class=""><div class="">On Jan 10, 2016, at 1:06 PM, Ryan Lovelett via swift-dev &lt;<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>&gt; wrote:<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><div class=""><span class="font" style="font-family:Helvetica"><span class="size" style="font-size:12px">typedef enum fe_type {</span></span><br class=""></div>
<div class=""><span class="font" style="font-family:Helvetica"><span class="size" style="font-size:12px">&nbsp;FE_QPSK,</span></span><br class=""></div>
<div class=""><span class="font" style="font-family:Helvetica"><span class="size" style="font-size:12px">&nbsp;FE_QAM,</span></span><br class=""></div>
<div class=""><span class="font" style="font-family:Helvetica"><span class="size" style="font-size:12px">&nbsp;FE_OFDM,</span></span><br class=""></div>
<div class=""><span class="font" style="font-family:Helvetica"><span class="size" style="font-size:12px">&nbsp;FE_ATSC</span></span><br class=""></div>
<div class=""><span class="font" style="font-family:Helvetica"><span class="size" style="font-size:12px">} fe_type_t;</span></span><br class=""></div>
</div>
</blockquote></div>
</div>
</div>
</blockquote><div class="">&nbsp;</div>
</div>
</div>
</blockquote></div>
<div class="">&nbsp;</div>
</div>
</div>
</blockquote></div>
<div class="">&nbsp;</div>
</div>
</div>
<div class=""><img src="https://www.fastmailusercontent.com/proxy/476b66b528f8dcfeaaf5c65def73bb0d6bcc943cc820fb9c858abeda082f79c1/8647470737a3f2f2777777e266163747d61696c65737562736f6e64756e647e236f6d6f20727f68797f256362353236373568303162393661363231373938366136603464333833366531646933303265346735336739323836326530343731313033333939303333336f283634373437303733373163366236623537323330333033323334333133303335623336343735623337353635663436373632373936343635623566353634373662373736363662366630373536356636633537303735663463316535343167343331373834393535663835383630353237333332363234303535373462323336343837303534333437356433333834363637353264336434623233363434373164303731373266313530333736326636643635333531343165303333353664346439363435353337343435393634373037313533353234343736643835383731363736366438333233323634643733346635333566316739353364336632663164343433333166336433363037363434373264363732643136353734643534383635333334323335373833313738353733373635353167316530333434333436373836313533643134373634623233323436343137353538363037323635333635346636333934346639373733393630373266303531363666323737363635373331343437313736333462323336343434313333373462323332343136333434343266346232333234353434373334326631363033316534363736373434373633343436643537346232333234333735663462323336343534316538333164313436353137373638333534363331343666393537333233353731663333343639363833346632363235313334353237373636343466363632373836313332363233373332353464393437343334393531373335383534663635373333363635303533363936373639363664356639343462333334343f2f60756e6/open" alt="" width="1" height="1" border="0" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;orphans:auto;text-align:start;text-indent:0px;text-transform:none;white-space:normal;widows:auto;word-spacing:0px;-webkit-text-stroke-width:0px;height:1px !important;width:1px !important;border-top-width:0px !important;border-right-width:0px !important;border-bottom-width:0px !important;border-left-width:0px !important;margin-top:0px !important;margin-right:0px !important;margin-bottom:0px !important;margin-left:0px !important;padding-top:0px !important;padding-right:0px !important;padding-bottom:0px !important;padding-left:0px !important;" class=""><span class="font" style="font-family:Helvetica"><span class="size" style="font-size:12px"><span class=""></span></span></span><span class="font" style="font-family:Helvetica"><span class="size" style="font-size:12px">_______________________________________________</span></span><br class=""></div>
<div class=""><span class="font" style="font-family:Helvetica"><span class="size" style="font-size:12px">swift-dev mailing list</span></span><br class=""></div>
<div class=""><a href="mailto:swift-dev@swift.org" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;orphans:auto;text-align:start;text-indent:0px;text-transform:none;white-space:normal;widows:auto;word-spacing:0px;-webkit-text-stroke-width:0px;" class="">swift-dev@swift.org</a><br class=""></div>
<div class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;orphans:auto;text-align:start;text-indent:0px;text-transform:none;white-space:normal;widows:auto;word-spacing:0px;-webkit-text-stroke-width:0px;" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a><br class=""></div>
</div>
</blockquote></div>
</blockquote><div class="">&nbsp;</div>
</div>
</div>
</blockquote></div>
</blockquote><div class="">&nbsp;</div>
</div>

</div></blockquote></div><br class=""></body></html>