[swift-evolution] Pitch: Import Objective-C Constants as Enums
Douglas Gregor
dgregor at apple.com
Mon Jan 18 13:21:17 CST 2016
Sent from my iPhone
> On Jan 18, 2016, at 9:41 AM, Philippe Hausler via swift-evolution <swift-evolution at swift.org> wrote:
>
> One note to those constants; there are a number of them (NSError domains in particular) that are intended to be a complement to the application space defines; such that your app could define a “MyGreatAppErrorDomain” and a “MyGreatFrameworkErrorDomain” in addition to the NSCocoaErrorDomain. So this isn’t exactly an enum but more so a non closed set of values. Enums are usually closed sets so that they can be switched upon exhaustively. Others would definitely improve the state of affairs for both objc and swift in that technically you could get a warning/error in objc if the type was not correct and there would be the swift ramifications you mentioned as well.
Enums imported from C are effectively open, because C APIs frequently have both internal constants and add new constants with subsequent releases. So string-backed enum a aren't all that different in many cases.
I don't think error domains are a great example of this, though. For one, they're something that's tied in with the error handling model, and should be somewhat invisible. My favorite example is the set of text styles one can provide then getting a UIFont, such as UIFontTextStyleHeadline. It's more enum-like and benefits greatly from the leading dot syntax enum a provide.
> Examples:
>
> @interface NSError : NSObject <NSCopying, NSSecureCoding>
> …
> - (instancetype)initWithDomain:(NSErrorDomain)domain code:(NSInteger)code userInfo:(nullable NSDictionary *)dict
> …
> @end
>
> this would mean that the following code would not work (or should produce a warning)
> [[NSError alloc] initWithDomain:@“MyAppDomain” code:42 userInfo:nil];
>
> moreover in swift:
> NSError(“MyAppDomain”, code:42, userInfo:nil) // would this even work?
>
> On the other hand it might be useful for the (desperately in need of some updating) NSLocale method
>
> @interface NSLocale : NSObject <NSCopying, NSSecureCoding>
> ...
> - (nullable id)objectForKey:(NSLocaleKey)key; // we could limit the keys accepted here to just the appropriate values in the “enum"
> ...
> @end
>
>
> From a brief survey of the APIs this would be useful for they mostly seem to be “dictionary based programming” cases which expose poorly in swift because they don’t have type safe return values. Adding more swift-friendly interfaces to those APIs is not off the table; because it is not just the keys that are the issue here but the return values too (see NSLocale’s example). Not to say we couldn’t do both, but would it be the case that if the un-typed return values that access by keys are replaced with better, more safe interfaces, this still be needed?
>
>
>> On Jan 18, 2016, at 9:13 AM, Jeff Kelley via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> I’ve got an in-progress proposal written up here:
>>
>> https://github.com/SlaunchaMan/swift-evolution/blob/import-objc-constants/proposals/0000-import-objc-constants.md
>>
>> I’ll leave it there for any additional comments/feedback, then submit later today.
>>
>>
>> Jeff Kelley
>>
>> SlaunchaMan at gmail.com | @SlaunchaMan | jeffkelley.org
>>
>>> On Jan 18, 2016, at 9:52 AM, Jeff Kelley <SlaunchaMan at gmail.com> wrote:
>>>
>>> Oh, that’s nice! Using the typedef would also allow Objective-C methods to be annotated with the type they expect while still allowing arbitrary strings for APIs where that’s necessary. Not only does that help in Swift—you wouldn’t have to use rawValue to pull the string values out when vending to the platform API—but it would give new developers to Apple platforms a head start on finding where those constants are defined in the headers.
>>>
>>> I’ll write this up as a formal proposal using the typedef method, though I’m still all ears for any other comments/suggestions.
>>>
>>>
>>> Jeff Kelley
>>>
>>> SlaunchaMan at gmail.com | @SlaunchaMan | jeffkelley.org
>>>
>>>>> On Jan 18, 2016, at 1:10 AM, Douglas Gregor <dgregor at apple.com> wrote:
>>>>>
>>>>>
>>>>> On Jan 17, 2016, at 7:13 PM, Jeff Kelley via swift-evolution <swift-evolution at swift.org> wrote:
>>>>>
>>>>> A lot of Cocoa APIs have long lists of constant values, typically NSStrings. I’d like to pitch a way to import them as enums with associated types. I can write up a full proposal if people think this is a good idea, but here’s my thinking:
>>>>>
>>>>> Let’s take the error domains in NSError.h for a quick example. These entries in the header:
>>>>>
>>>>>> FOUNDATION_EXPORT NSString *const NSCocoaErrorDomain;
>>>>>> FOUNDATION_EXPORT NSString *const NSPOSIXErrorDomain;
>>>>>> FOUNDATION_EXPORT NSString *const NSOSStatusErrorDomain;
>>>>>> FOUNDATION_EXPORT NSString *const NSMachErrorDomain;
>>>>>
>>>>>
>>>>> turn into this in the Swift interface:
>>>>>
>>>>>> public let NSCocoaErrorDomain: String
>>>>>> public let NSPOSIXErrorDomain: String
>>>>>> public let NSOSStatusErrorDomain: String
>>>>>> public let NSMachErrorDomain: String
>>>>>
>>>>> What I’m proposing is a way to import those as an enum instead. Similar to how we mark sections of Objective-C code with NS_ASSUME_NONNULL_BEGIN, we could mark it with something like NS_CASE_LIST_BEGIN. Then, this code:
>>>>>
>>>>>> NS_CASE_LIST_BEGIN;
>>>>>>
>>>>>> FOUNDATION_EXPORT NSString *const NSCocoaErrorDomain;
>>>>>> FOUNDATION_EXPORT NSString *const NSPOSIXErrorDomain;
>>>>>> FOUNDATION_EXPORT NSString *const NSOSStatusErrorDomain;
>>>>>> FOUNDATION_EXPORT NSString *const NSMachErrorDomain;
>>>>>>
>>>>>> NS_CASE_LIST_END;
>>>>>
>>>>> would be imported as follows:
>>>>>
>>>>>> enum ErrorDomain : String {
>>>>>> case Cocoa
>>>>>> case POSIX
>>>>>> case OSStatus
>>>>>> case Mach
>>>>>> }
>>>>>
>>>>> 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?
>>>>
>>>> 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.,
>>>>
>>>> typedef NSString * NSErrorDomain __attribute__((enum(string)));
>>>>
>>>> FOUNDATION_EXPORT NSErrorDomain const NSCocoaErrorDomain;
>>>> FOUNDATION_EXPORT NSErrorDomain const NSPOSIXErrorDomain;
>>>> FOUNDATION_EXPORT NSErrorDomain const NSOSStatusErrorDomain;
>>>> FOUNDATION_EXPORT NSErrorDomain const NSMachErrorDomain;
>>>>
>>>> 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.
>>>>
>>>> Call that a +1 from me on your idea :)
>>>>
>>>> - Doug
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160118/7546abc9/attachment.html>
More information about the swift-evolution
mailing list