[swift-evolution] Pitch: Import Objective-C Constants as Enums

Jeff Kelley slaunchaman at gmail.com
Fri Jan 29 16:09:13 CST 2016


Thanks for the feedback, everyone! I’ve updated the proposal <https://github.com/SlaunchaMan/swift-evolution/blob/import-objc-constants/proposals/0000-import-objc-constants.md> again to use both _attribute__((swift_wrapper(struct))) and __attribute__((swift_wrapper(struct))). Like Doug, I think having both fits the largest number of use cases.

I also changed the accessors on struct items to use var instead of let, which matches how option set types are imported.

> On Jan 29, 2016, at 1:48 PM, Douglas Gregor <dgregor at apple.com> wrote:
> 
> I think you need to clarify a few things:
> 
> (1) How are the names of the constants transformed? It looks like you’re chopping off the common prefix between the type name and the enum constant name. Should the result have initial lowercase letters?

I’m removing both common prefixes and suffixes, if any. That way, both the “NS” and “ErrorDomain” of “NSErrorDomain” get chopped off. The initial result is uppercase to match existing imports (e.g. OptionSetType):

struct NSEnumerationOptions : OptionSetType <file:///Users/jeff/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.iOS.docset/Contents/Resources/Documents/documentation/Swift/Reference/Swift_OptionSetType_Protocol/index.html#//apple_ref/swift/intf/s:PSs13OptionSetType> {
    init(rawValue rawValue: UInt <file:///Users/jeff/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.iOS.docset/Contents/Resources/Documents/documentation/Swift/Reference/Swift_UInt_Structure/index.html#//apple_ref/swift/struct/s:Su>)
    static var Concurrent: NSEnumerationOptions <file:///Users/jeff/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.iOS.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/index.html#//apple_ref/swift/struct/c:@E@NSEnumerationOptions> { get }
    static var Reverse: NSEnumerationOptions <file:///Users/jeff/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.iOS.docset/Contents/Resources/Documents/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/index.html#//apple_ref/swift/struct/c:@E@NSEnumerationOptions> { get }
}

> (2) What happens if additional constants are defined in a different module? (I assume that the new “static let”s go into an extension)

I don’t think it’s possible for another module to add cases to an enum, so for those, we’d have to make them structs instead and use an extension. There’s an example of this for structs in the new proposal.

Thanks for the feedback! This proposal is now better than ever.


Jeff Kelley

SlaunchaMan at gmail.com | @SlaunchaMan <https://twitter.com/SlaunchaMan> | jeffkelley.org <http://jeffkelley.org/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160129/4d56b2e9/attachment.html>


More information about the swift-evolution mailing list