Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to add another item to an existing enum type?

I wonder whether it is possible to add/append another item to an existing enum type (part of a framework)?

Something like this: We have the enum type

  typedef enum {  
    UIModalTransitionStyleCoverVertical = 0,  
    UIModalTransitionStyleFlipHorizontal,
    UIModalTransitionStyleCrossDissolve,
    UIModalTransitionStylePartialCurl,
 } UIModalTransitionStyle;  

Now I want to append or add to this set an item like UIModalTransitionStyleCoverVerticalFlipped. Can something like this be accomplished?

like image 259
user325746 Avatar asked Jun 26 '10 13:06

user325746


People also ask

Can you add values to enum?

You can add a new value to a column of data type enum using ALTER MODIFY command. If you want the existing value of enum, then you need to manually write the existing enum value at the time of adding a new value to column of data type enum.

Is it possible to extend an enum?

The short answer is no, you can't extend enums because TypeScript offers no language feature to extend them.

Is it possible to extend enum in Java?

No, we cannot extend an enum in Java. Java enums can extend java. lang. Enum class implicitly, so enum types cannot extend another class.

Can you add to an enum type in run time?

If the enum can be defined at program startup, you place the enum in a separate assembly and use a bootstrapper that will recompile the enum, overwriting the old version, and then launch the actual application. It's not the cleanest method, but it works.


3 Answers

You can force new element to have the same type as the enum, but you can't extend it in a subclass. header file:

extern const UIModalTransitionStyle UIModalTransitionStyleCoverVerticalFlipped;

implementation file:

const UIModalTransitionStyle UIModalTransitionStyleCoverVerticalFlipped = 10;

Make sure to give some space in case the framework is extended, so that you don't have conflicts. This is a bit of a hack, but it will get rid of compiler errors and warnings.

like image 112
epooch Avatar answered Oct 20 '22 18:10

epooch


To do it, you have to modify the original type definition to include the new value:

typedef enum {  
    UIModalTransitionStyleCoverVertical = 0,  
    UIModalTransitionStyleFlipHorizontal,
    UIModalTransitionStyleCrossDissolve,
    UIModalTransitionStylePartialCurl,
    UIModalTransitionStyleCoverVerticalFlipped
} UIModalTransitionStyle;

Otherwise, you can take a chance on its not working, and define it separately:

typedef enum {  
    UIModalTransitionStyleCoverVertical = 0,  
    UIModalTransitionStyleFlipHorizontal,
    UIModalTransitionStyleCrossDissolve,
    UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;

typedef enum { 
    UIModalTransitionStyleCoverVerticalFlipped =
        UIModalTransitionStylePartialCurl + 1
} ExtendedUIModalTransitionStyle;

A variable that could hold the original enumeration will usually also work perfectly fine when/if you assign the new value as well (in a typical case, it'll just be an int) -- but it's not guaranteed. At least in theory, the implementation can/could assign few enough bits to hold that enumeration that it adding more values this way wouldn't work. It could also do range checking so assigning any out of range value wouldn't be allowed. Neither of these is at all common, so from a practical viewpoint it's probably not a problem -- but from a theoretical viewpoint, nothing really guarantees that code like this will work.

like image 30
Jerry Coffin Avatar answered Oct 20 '22 17:10

Jerry Coffin


Maybe this can help you:

typedef NS_ENUM(NSInteger, BaseType) {
    BaseTypeCase1,
    BaseTypeCase2,
    BaseTypeSize
};

typedef NS_ENUM(NSInteger, SubType) {
    SubTypeCase1 = BaseTypeSize,
    SubTypeCase2
};

Now you can switch on SubType knowing the values are unique.

If you don't have access to BaseType, you could set SubTypeCase1 to BaseType's last item + 1.

Downside is, you can't declare a method that takes a SubType and pass to it a BaseType without getting a compiler warning. So you need to declare your methods to take NSIntegers in order silence that warning. Also, it feels weird when you need to declare a parameter of SubType and be able to pass in a BaseType.

like image 1
Siamaster Avatar answered Oct 20 '22 17:10

Siamaster