[swift-evolution] Proposal: Allow @objc(name) on enum declarations

Douglas Gregor dgregor at apple.com
Fri Dec 11 16:39:14 CST 2015


> On Dec 11, 2015, at 1:19 PM, Kevin Ballard via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I've now submitted this proposal as a PR: https://github.com/apple/swift-evolution/pull/50 <https://github.com/apple/swift-evolution/pull/50>

Thanks for writing this up!

I was actually surprised that this didn’t already work; @objc is supposed to be able to rename effectively anything at this point. Given that, and that another core team member has already +1’d, we can skip the evolution process and call this a bug fix. Want to contribute a fix?

> I listed one open question:
> 
> Should the generated Obj-C declarations use the swift_name attribute to indicate the Swift type it came from? Proposal SE-0005 generalizes swift_name to apply to any arbitrary C or Obj-C entity, so it will be legal to put on enums.
> 
> Labelling it makes it clear to the reader what the Swift equivalent is, but I'm unsure if there's any downsides to doing this.

Yes, it makes sense to use the swift_name attribute here. If you’re implementing your proposal, check out lib/PrintAsObjC/PrintAsObjC.cpp and how we use the SWIFT_COMPILE_NAME macro for this in the generated header.

	- Doug

> 
> -Kevin Ballard
> 
> On Thu, Dec 10, 2015, at 09:45 PM, Chris Lattner wrote:
>> 
>>> On Dec 9, 2015, at 3:18 PM, Kevin Ballard via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> Swift allows for placing @objc on an enum that has an Int raw type in order to expose it to Obj-C. But it doesn't currently let you rename the enum when exposing it to Obj-C. This is particularly problematic when exposing a Swift enum that's nested in a struct/class, as the nesting resolves ambiguity in Swift but is not present in Obj-C.
>> 
>> +1 to this idea.
>> 
>> -Chris
>> 
>> 
>>> 
>>> Example:
>>> 
>>> import Foundation
>>> 
>>> class Foo: NSObject {
>>>   @objc enum Bar: Int {
>>>       case One, Two
>>>   }
>>> }
>>> 
>>> This generates the following:
>>> 
>>> SWIFT_CLASS("_TtC7unnamed3Foo")
>>> @interface Foo : NSObject
>>> - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
>>> @end
>>> 
>>> typedef SWIFT_ENUM(NSInteger, Bar) {
>>> BarOne = 0,
>>> BarTwo = 1,
>>> };
>>> 
>>> I'd like to resolve this by saying @objc(FooBar) but that emits an error.
>>> 
>>> I'm also going to submit a separate proposal saying we should change the default naming here so the enum is named FooBar, but these two proposals go hand-in-hand (there are cases where you might want to rename a root-level enum, to add a prefix for disambiguation in Obj-C, or you may want to selectively opt out of the proposed renaming rules by forcing your nested enum to use just its name in Obj-C).
>>> 
>>> -Kevin Ballard
>>> _______________________________________________
>>> 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/20151211/036eab10/attachment.html>


More information about the swift-evolution mailing list