[swift-evolution] [pitch] Make swift enum string available to Objc

Derrick Ho wh1pch81n at gmail.com
Mon Feb 20 19:05:26 CST 2017


Swift Enum strings should not translate into objective c global static
strings.

As I wrote in my proposal

(
https://github.com/wh1pch81n/swift-evolution/blob/swift-enum-objc/proposals/NNNN-Swift-enum-strings-ported-to-Objective-c.md
)

, we need to support a failable initializer.

I chose @objcstring to keep it consistent with how it would be used with
struct for NS_EXTENSIBLE_STRING_ENUM. While an enum can inherit from
String, a struct does not. So the evidence that this is meant to be used
for strings is not there. That is why I suggest @objcstring to make that
explicit and consistent between the two kinds.



On Mon, Feb 20, 2017 at 7:33 PM Xiaodi Wu <xiaodi.wu at gmail.com> wrote:

> Agree. If technical limitations do not prohibit it, Kevin's idea seems the
> more elegant.
>
>
> On Mon, Feb 20, 2017 at 6:28 PM, Kevin Nattinger via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> I don’t think we need/want to add `@objcstring` or anything like that, but
> I do think we should let String enums be @objc (currently it’s int types
> only) and imported as const refs.
>
> // Already works
> @objc enum IntEnum: Int {
>     case foo = 1
>     case bar = 2
> }
>
> // "not an integer type"
> // Should be allowed.
> @objc enum StrEnum: String {
>     case foo
>     case bar = "baz"
> }
>
> Becomes
>
> // Current
> typedef NS_ENUM(NSInteger, IntEnum) {
>     IntEnumFoo = 1,
>     IntEnumBar = 2
> };
>
> // Proposed (static or extern, depending on implementation)
> NSString *const StrEnumFoo = @“foo";
> NSString *const StrEnumBar = @“baz";
>
> In fact, I’d go a step further and say any RawRepresentable enum as a
> globally initialized const in objc should be allowed the same treatment.
> Though the only other type that comes to mind is float/double.
>
> On Feb 20, 2017, at 4:07 PM, Derrick Ho via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> Swift should not forsake objective-c.  At least not when it comes enum
> strings.  Although swift enums are suppose to be swift only, I think we
> should add a new attribute to slightly relax that.  I think a good
> attribute would be @objcstring.
>
> By adding @objcstring, an objective-c exclusive class will be generated.
>
> @objcstring
> enum Planet {
>   case Jupiter
> }
>
> I have written up a proposal with more details on what it would look for
> objective-c.
>
>
> https://github.com/wh1pch81n/swift-evolution/blob/swift-enum-objc/proposals/NNNN-Swift-enum-strings-ported-to-Objective-c.md
>
> If no one objects to this proposal I'll submit it.
>
> **notes: I am reviving this discussion so that I may submit this for Swift
> 4 stage 2
> _______________________________________________
> 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/20170221/30db440b/attachment.html>


More information about the swift-evolution mailing list