<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 22:10 , Douglas Gregor 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 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=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">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=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="">A lot of Cocoa APIs have long lists of constant values, typically<span class="Apple-converted-space"> </span><font face="Menlo" class="">NSString</font>s. I’d like to pitch a way to import them as<span class="Apple-converted-space"> </span><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<span class="Apple-converted-space"> </span><font face="Menlo" class="">NSError.h</font><span class="Apple-converted-space"> </span>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<span class="Apple-converted-space"> </span><font face="Menlo" class="">enum</font><span class="Apple-converted-space"> </span>instead. Similar to how we mark sections of Objective-C code with<span class="Apple-converted-space"> </span><font face="Menlo" class="">NS_ASSUME_NONNULL_BEGIN</font>, we could mark it with something like<span class="Apple-converted-space"> </span><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="" 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;"><div class="" 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;">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="" 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;"><br class=""></div><div class="" 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;"> typedef NSString * NSErrorDomain __attribute__((enum(string)));</div><div class="" 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;"><br class=""></div><div class="" 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;"> FOUNDATION_EXPORT NSErrorDomain const NSCocoaErrorDomain;</div><div class="" 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;"><div class=""> FOUNDATION_EXPORT NSErrorDomain const NSPOSIXErrorDomain;</div></div><div class="" 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;"><div class=""> FOUNDATION_EXPORT NSErrorDomain const NSOSStatusErrorDomain;</div></div><div class="" 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;"><div class=""> FOUNDATION_EXPORT NSErrorDomain const NSMachErrorDomain;</div></div><div class="" 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;"><br class=""></div><div class="" 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;">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="" 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;"><br class=""></div><div class="" 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;">Call that a +1 from me on your idea :)</div></div></blockquote><br class=""></div><div>I still have reservations about this:</div><div><br class=""></div><div>- Most of these strings are not things you switch on, making the enum-ness not particularly interesting. I'd be happier with a RawRepresentable struct.</div><div>- Our current prefix-stripping logic relies on being able to see all the cases. You can do this with an enum because they're all declared in one block, but string constants they may cross multiple files. This isn't impossible to deal with, but I think it's counterintuitive. (An alternative would be to only use the type name for stripping each value individually.)</div><div>- I'm not sure where the line is between "an open set of choices represented as strings" and "strings with some defaults defined". (Very few of these are truly closed sets; if we add a new UIFont attribute next year, it should prefix-strip like everything else.)</div><div>- As a nitpick, I think a typedef for 'NSString' rather than 'NSString *' would be preferred, so that the '*' still shows up in the declaration in Objective-C. This is just a feeling though, and maybe it's just clinging to the way things are done now.</div><div><br class=""></div>Jordan</body></html>