Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Adding Section on Boxed Enums #116

Closed
wants to merge 7 commits into from

Conversation

spacedrabbit
Copy link

Creating PR for issue: #93 to explain the proposal. Let me know if this is worth explicitly mentioning/adding in or if this needs syntax changes.

closes #93

@@ -322,6 +323,25 @@ typedef NS_ENUM(NSInteger, NYTAdRequestState) {
};
```

## Boxing Enumerated Types

Althought `enum`s are integral values, they cannot be directly used as [boxed literals](http://clang.llvm.org/docs/ObjectiveCLiterals.html#boxed-enums). So when creating a dictionary using `enum`s, they should be written as boxed literals instead of their corresponding `NSNumber` expression.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "Althought"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write the second sentence…

When boxing enums (for example, when using them in a Cocoa collection) they should be boxed as NSNumbers using the shorthand @() literal expression, not as the [NSNumber numberWith…:] methods.

This eliminates the need to look up the underlying type of the enum in order to box it correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "integral" isn't the right word. I would say "integer".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d beg to differ. Integral makes sense in this context:

screen shot 2015-04-22 at 2 59 22 pm

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

screen shot 2015-04-22 at 14 58 41

I don't feel strongly either way, except to note that an enum could be backed by a type which isn't strictly speaking an integer in C.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, my dictionary fail

@@ -322,6 +323,27 @@ typedef NS_ENUM(NSInteger, NYTAdRequestState) {
};
```

## Boxing Enumerated Types

Although `enum`s are integral values, they cannot be directly used as [boxed literals](http://clang.llvm.org/docs/ObjectiveCLiterals.html#boxed-enums). When boxing `enum`s (for example when using them in a Cocoa collection) they should be boxed as `NSNumber`s using the shorthand `@()` literal expression, not as the [NSNumber numberWith...:]` methods.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…, not as the [NSNumber numberWith...:]` methods.

You're missing the backtick before "[NSNumber" here.


**For example:**
```objc
typedef NS_ENUM(NSInteger, NYTDaysOfTheWeek) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, for proper integration with Swift the name of the enum and the values should share the same prefix. Such as:

typedef NS_ENUM(NSInteger, NYTWeekday) {
    NYTWeekdayMonday = 0,
    NYTWeekdayTuesday
}; 

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. ❤️

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattbischoff not familiar with the Swift integration issue you're bringing up, could you point me in a direction regarding that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattbischoff
Copy link
Contributor

Thinking about this more, I’m not sure it belongs in the style guide. It doesn’t really cover any new ground stylistically. What do you all think?

@loganmoseley
Copy link
Contributor

I believe that by, "they cannot be directly used as boxed literals," @spacedrabbit is referencing Clang's (vaguely stated) decision to enforce @(AVAudioQualityMin) and disallow @AVAudioQualityMin. It's not a question of style, the latter does not compile.

Considering boxed literals—@(AVAudioQualityMin)—over the long-hand NSNumber creation methods—[NSNumber numberWithInteger:AVAudioQualityMin]—there is nothing special about enums. All numbers are boxed via the former syntax. If anything, usage of @(x) merits a line in the examples of Literals.

@mattbischoff
Copy link
Contributor

What’s the status of this request?

@loganmoseley
Copy link
Contributor

I think this suggestion is subsumed by Literals and enforced by Clang. I would not merge this PR.

Anybody else?

@mattbischoff
Copy link
Contributor

Makes sense to me.

@Veronique Veronique closed this Dec 8, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Including Boxed enums in "Literals" Section?
6 participants