<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 17, 2016, at 7:13 PM, Jeff Kelley via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; 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=""><div class="">A lot of Cocoa APIs have long lists of constant values, typically <font face="Menlo" class="">NSString</font>s. I’d like to pitch a way to import them as <font face="Menlo" class="">enum</font>s with associated types. I can write up a full proposal if people think this is a good idea, but here’s my thinking:</div><div class=""><br class=""></div><div class="">Let’s take the error domains in <font face="Menlo" class="">NSError.h</font> for a quick example. These entries in the header:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""><blockquote type="cite" class="">FOUNDATION_EXPORT NSString *const&nbsp;NSCocoaErrorDomain;<br class="">FOUNDATION_EXPORT NSString *const&nbsp;NSPOSIXErrorDomain;<br class="">FOUNDATION_EXPORT NSString *const&nbsp;NSOSStatusErrorDomain;<br class="">FOUNDATION_EXPORT NSString *const&nbsp;NSMachErrorDomain;</blockquote></font></div><div class=""><br class=""></div><div class="">turn into this in the Swift interface:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""></font></div><blockquote type="cite" class=""><div class=""><font face="Menlo" class="">public&nbsp;let&nbsp;NSCocoaErrorDomain:&nbsp;String<br class="">public&nbsp;let&nbsp;NSPOSIXErrorDomain:&nbsp;String<br class="">public&nbsp;let&nbsp;NSOSStatusErrorDomain:&nbsp;String<br class="">public&nbsp;let&nbsp;NSMachErrorDomain:&nbsp;String</font></div></blockquote><div class=""><br class=""></div><div class="">What I’m proposing is a way to import those as an <font face="Menlo" class="">enum</font> instead. Similar to how we mark sections of Objective-C code with <font face="Menlo" class="">NS_ASSUME_NONNULL_BEGIN</font>, we could mark it with something like <font face="Menlo" class="">NS_CASE_LIST_BEGIN</font>. Then, this code:</div><div class=""><br class=""></div><div class=""></div><blockquote type="cite" class=""><div class=""><font face="Menlo" class="">NS_CASE_LIST_BEGIN;</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">FOUNDATION_EXPORT NSString *const&nbsp;NSCocoaErrorDomain;<br class="">FOUNDATION_EXPORT NSString *const&nbsp;NSPOSIXErrorDomain;<br class="">FOUNDATION_EXPORT NSString *const&nbsp;NSOSStatusErrorDomain;<br class="">FOUNDATION_EXPORT NSString *const&nbsp;NSMachErrorDomain;</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">NS_CASE_LIST_END;</font></div></blockquote><div class=""><br class=""></div><div class="">would be imported as follows:</div><div class=""><br class=""></div><font face="Menlo" class=""></font><div class=""></div><blockquote type="cite" class=""><font face="Menlo" class="">enum&nbsp;ErrorDomain :&nbsp;String&nbsp;{<br class="">&nbsp; &nbsp;&nbsp;case&nbsp;Cocoa<br class="">&nbsp; &nbsp;&nbsp;case&nbsp;POSIX<br class="">&nbsp; &nbsp;&nbsp;case&nbsp;OSStatus</font><div class=""><font face="Menlo" class="">&nbsp; &nbsp; case Mach<br class="">}</font></div></blockquote><div class=""><br class=""></div><div class="">I can think of a lot of areas in Cocoa where these APIs could make things much more type-safe in Swift. Is this a good idea? Would people use this?<br class=""></div></div></div></blockquote></div><br class=""><div class="">FWIW, this has come up a number of times in discussions among Swift developers (although not, IIRC, on swift-evolution). Our current favored way to write this in (Objective-)C would be with a new typedef of NSString * that has some special attribute on it, e.g.,</div><div class=""><br class=""></div><div class="">&nbsp; typedef NSString * NSErrorDomain __attribute__((enum(string)));</div><div class=""><br class=""></div><div class="">&nbsp; FOUNDATION_EXPORT NSErrorDomain const NSCocoaErrorDomain;</div><div class=""><div class="">&nbsp; FOUNDATION_EXPORT NSErrorDomain const NSPOSIXErrorDomain;</div></div><div class=""><div class="">&nbsp; FOUNDATION_EXPORT NSErrorDomain const NSOSStatusErrorDomain;</div></div><div class=""><div class="">&nbsp; FOUNDATION_EXPORT NSErrorDomain const NSMachErrorDomain;</div></div><div class=""><br class=""></div><div class="">The typedef would import as a String-backed enum and all of the string constants declared with that typedef within the same module as the typedef would become cases of that enum. String constants declared with that typedef in a *different* module would become “static lets” within extensions of the String-backed enum.</div><div class=""><br class=""></div><div class="">Call that a +1 from me on your idea :)</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>- Doug</div><div class=""><br class=""></div></body></html>