<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 <<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=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 NSCocoaErrorDomain;<br class="">FOUNDATION_EXPORT NSString *const NSPOSIXErrorDomain;<br class="">FOUNDATION_EXPORT NSString *const NSOSStatusErrorDomain;<br class="">FOUNDATION_EXPORT NSString *const 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 let NSCocoaErrorDomain: String<br class="">public let NSPOSIXErrorDomain: String<br class="">public let NSOSStatusErrorDomain: String<br class="">public let NSMachErrorDomain: 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 NSCocoaErrorDomain;<br class="">FOUNDATION_EXPORT NSString *const NSPOSIXErrorDomain;<br class="">FOUNDATION_EXPORT NSString *const NSOSStatusErrorDomain;<br class="">FOUNDATION_EXPORT NSString *const 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 ErrorDomain : String {<br class=""> case Cocoa<br class=""> case POSIX<br class=""> case OSStatus</font><div class=""><font face="Menlo" class=""> 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=""> typedef NSString * NSErrorDomain __attribute__((enum(string)));</div><div class=""><br class=""></div><div class=""> FOUNDATION_EXPORT NSErrorDomain const NSCocoaErrorDomain;</div><div class=""><div class=""> FOUNDATION_EXPORT NSErrorDomain const NSPOSIXErrorDomain;</div></div><div class=""><div class=""> FOUNDATION_EXPORT NSErrorDomain const NSOSStatusErrorDomain;</div></div><div class=""><div class=""> 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>