What is the reason define unknown enum in separate class?

sarunw

In Apple Lister example https://developer.apple.com/library/ios/samplecode/Lister/Listings/Objective_C_ListerKit_AAPLListInfo_m.html#//apple_ref/doc/uid/TP40014701-Objective_C_ListerKit_AAPLListInfo_m-DontLinkElementID_35

they have this code to define additional undefined state of color enum

#define AAPLListColorUndefined ((AAPLListColor)-1)
...
#undef AAPLListColorUndefined

but why don't they just add this to the class where they define others color

typedef NS_ENUM(NSInteger, AAPLListColor) {
    AAPLListColorGray = 0,
    AAPLListColorBlue,
    AAPLListColorGreen,
    AAPLListColorYellow,
    AAPLListColorOrange,
    AAPLListColorRed
};

Is there a reason behind this ? or just a personal style without any benefit.

Matt Gibson

Note that AAPLListColorUndefined is used only in the file it's declared (and is even specifically undefined at the end of that file.) It's basically a helper value that's only used in that class (to keep track of whether the colour has been set yet.) Unlike the other values, it's only useful in that one class. So, that's design reason one: it's not really a colour value; it's basically just a flag used in that specific class.

A second reason is that the compiler will check NS_ENUM values for completeness in switch statements. Try adding AAPLListColorUndefined into the enum itself—you'll now see compiler warnings for switch statements like this:

// Warning: Enumeration value 'AAPLListColorUndefined' not handled in switch
switch (listColor) {
    case AAPLListColorGray:     return _grayColor;
    case AAPLListColorBlue:     return _blueColor;
    case AAPLListColorGreen:    return _greenColor;
    case AAPLListColorYellow:   return _yellowColor;
    case AAPLListColorOrange:   return _orangeColor;
    case AAPLListColorRed:      return _redColor;
}

...because the compiler can see that the undefined value isn't being handled. As the value can only be undefined in that one specific class, it makes sense not to have it available in the NS_ENUM for the other classes, as that would force you to handle it, when you know your colour value can't logically can't ever be undefined in them. (In the other files, a default grey colour is used, so there's no need for an "undefined" value—the colour is always defined.)

So, that's kind of the "flip side" of the design reason: as well as only being useful in the one file it's defined, it's specifically not useful in the files it's not being used in.

This is very tidy code, I'd say. If you were to add the undefined colour as part of the enum, then you'd need to do something in every switch statement that used the enum, even if it was only having a default: case that did nothing, or raised an error that you knew would never happen.

Or, you could get away without having the undefined value at all in AAPLListInfo.m by setting the colour property to a default value, say grey, like the other files, but then you'd need an extra flag—a boolean, probably—to indicate whether the list info has been fetched.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the reason define unknown enum in separate class?

From Dev

What is a reason of getting error: "Unknown property '' on class MyBean"?

From Dev

What is a reason of getting error: "Unknown property '' on class MyBean"?

From Dev

What is the reason for marking an enum with the [Flags] attribute?

From Dev

Define what class contains

From Dev

Hibernate Exception: Unknown name value for enum class

From Dev

Separate header with Enum Class and operator overload

From Dev

Separate header with Enum Class and operator overload

From Dev

Define std::hash for enum member of class template

From Dev

Define static anonymous enum outside the class

From Dev

Define static anonymous enum outside the class

From Dev

What is the difference between enum struct and enum class?

From Dev

Sklearn GridSearchCV, class_weight not working for unknown reason :(

From Dev

Sklearn GridSearchCV, class_weight not working for unknown reason :(

From Dev

What are the allowed types for an enum (class)?

From Dev

What's the reason for initializing fields inside class?

From Dev

What is the reason for having 3 Class loaders in Java

From Dev

What's the reason for using lambda expressions to define functions in Scheme?

From Dev

How can I reference a static class with a separate enum class in Java?

From Dev

Is it possible to declare constexpr class in a header and define it in a separate .cpp file?

From Dev

Javascript: Class constructor basics - define property in separate method

From Dev

NaN for an unknown reason

From Dev

Oracle dead with unknown reason

From Dev

Getting a TypeError for unknown reason

From Dev

StringIndexOutOfBoundException for unknown reason

From Dev

MongoDB stops unknown reason

From Dev

ArrayIndexOutOfBoundsException for unknown reason

From Dev

JFrame in separate class, what about the ActionListener?

From Dev

What things are needed for a class to define a vector of it?

Related Related

  1. 1

    What is the reason define unknown enum in separate class?

  2. 2

    What is a reason of getting error: "Unknown property '' on class MyBean"?

  3. 3

    What is a reason of getting error: "Unknown property '' on class MyBean"?

  4. 4

    What is the reason for marking an enum with the [Flags] attribute?

  5. 5

    Define what class contains

  6. 6

    Hibernate Exception: Unknown name value for enum class

  7. 7

    Separate header with Enum Class and operator overload

  8. 8

    Separate header with Enum Class and operator overload

  9. 9

    Define std::hash for enum member of class template

  10. 10

    Define static anonymous enum outside the class

  11. 11

    Define static anonymous enum outside the class

  12. 12

    What is the difference between enum struct and enum class?

  13. 13

    Sklearn GridSearchCV, class_weight not working for unknown reason :(

  14. 14

    Sklearn GridSearchCV, class_weight not working for unknown reason :(

  15. 15

    What are the allowed types for an enum (class)?

  16. 16

    What's the reason for initializing fields inside class?

  17. 17

    What is the reason for having 3 Class loaders in Java

  18. 18

    What's the reason for using lambda expressions to define functions in Scheme?

  19. 19

    How can I reference a static class with a separate enum class in Java?

  20. 20

    Is it possible to declare constexpr class in a header and define it in a separate .cpp file?

  21. 21

    Javascript: Class constructor basics - define property in separate method

  22. 22

    NaN for an unknown reason

  23. 23

    Oracle dead with unknown reason

  24. 24

    Getting a TypeError for unknown reason

  25. 25

    StringIndexOutOfBoundException for unknown reason

  26. 26

    MongoDB stops unknown reason

  27. 27

    ArrayIndexOutOfBoundsException for unknown reason

  28. 28

    JFrame in separate class, what about the ActionListener?

  29. 29

    What things are needed for a class to define a vector of it?

HotTag

Archive