<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>Do we have any attributes that are valid on enum cases today? If so, how do they handle this scenario?<br></div>
<div>&nbsp;</div>
<div>I'm inclined to agree with you that it should be an error. It's reasonable to assume that an attribute on the declaration applies to all cases in the declaration, which is fine for most attributes, it just doesn't work for @objc(name) because it produces a name collision.<br></div>
<div>&nbsp;</div>
<div>I suspect the simplest approach here is to just figure out what the error message will be if you say<br></div>
<div>&nbsp;</div>
<div>@objc(Foo) case Foo<br></div>
<div>@objc(Foo) case Bar<br></div>
<div>&nbsp;</div>
<div>and use the same error if you say<br></div>
<div>&nbsp;</div>
<div>@objc(Foo) case Foo, Bar<br></div>
<div>&nbsp;</div>
<div>-Kevin Ballard</div>
<div>&nbsp;</div>
<div>On Thu, Dec 10, 2015, at 07:22 AM, Harlan Haskins wrote:<br></div>
<blockquote type="cite"><div>An error seems reasonable here.<br></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family:Menlo">Error: Ambiguous Objective-C case name for ‘.Two’.</span><br></div>
<div>&nbsp;</div>
<div>And then the Fix-It is just to put the second case on a new line and provide the <span class="font" style="font-family:Menlo">@objc(&lt;#name#&gt;)</span>&nbsp;completion.<br></div>
<div><div>&nbsp;</div>
<div><blockquote type="cite"><div>On Dec 10, 2015, at 1:56 AM, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div>
<div>&nbsp;</div>
<div><div><div>Does it make sense to put @objc(name) on enum cases? Such as<br></div>
<div>&nbsp;</div>
<div>@objc enum Foo: Int {<br></div>
<div> &nbsp;&nbsp;&nbsp;@objc(kFooOne) case One<br></div>
<div> &nbsp;&nbsp;&nbsp;@objc(kFooTwo) case Two<br></div>
<div>}<br></div>
<div>&nbsp;</div>
<div>The only real problem here is what do we do if you say<br></div>
<div>&nbsp;</div>
<div>@objc(kFooOne) case One, Two<br></div>
<div>&nbsp;</div>
<div>Either we just apply it to the One variant and ignore the Two variant, possibly with a warning, or we emit an error.<br></div>
<div>&nbsp;</div>
<div>-Kevin<br></div>
<div>&nbsp;</div>
<div>On Wed, Dec 9, 2015, at 06:01 PM, Jordan Rose wrote:<br></div>
<blockquote type="cite"><div>&nbsp;</div>
<blockquote type="cite"><div>On Dec 9, 2015, at 15:18, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div>
<div>&nbsp;</div>
<div>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.<br></div>
<div>&nbsp;</div>
<div>Example:<br></div>
<div>&nbsp;</div>
<div>import Foundation<br></div>
<div>&nbsp;</div>
<div>class Foo: NSObject {<br></div>
<div> &nbsp;&nbsp;@objc enum Bar: Int {<br></div>
<div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case One, Two<br></div>
<div> &nbsp;&nbsp;}<br></div>
<div>}<br></div>
<div>&nbsp;</div>
<div>This generates the following:<br></div>
<div>&nbsp;</div>
<div>SWIFT_CLASS("_TtC7unnamed3Foo")<br></div>
<div>@interface Foo : NSObject<br></div>
<div>- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;<br></div>
<div>@end<br></div>
<div>&nbsp;</div>
<div>typedef SWIFT_ENUM(NSInteger, Bar) {<br></div>
<div> BarOne = 0,<br></div>
<div> BarTwo = 1,<br></div>
<div>};<br></div>
<div>&nbsp;</div>
<div>I'd like to resolve this by saying @objc(FooBar) but that emits an error.<br></div>
<div>&nbsp;</div>
<div>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).<br></div>
</blockquote><div>&nbsp;</div>
<div>+1 to being able to rename @objc enums. I'm a little surprised we even allow exposing nested types to Objective-C, and am not at all sure the printer is set up to handle that correctly, but the renaming is independently useful.<br></div>
<div>&nbsp;</div>
<div>Jordan<br></div>
</blockquote><div>_______________________________________________<br></div>
<div>swift-evolution mailing list<br></div>
<div><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br></div>
<div>https://lists.swift.org/mailman/listinfo/swift-evolution<br></div>
</div>
</div>
</blockquote></div>
</div>
</blockquote><div>&nbsp;</div>
</body>
</html>