Questions tagged [enums]

A data type consisting of a set of named values called elements, members or enumerators of the type.

This tag is for questions related to enumeration, enumerated-types (or enums) as related to programming.

In computer programming, an enumerated type (also called enumeration or enum) is a data type consisting of a set of named values called elements, members or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value.

From the wikipedia article on enumerated type

Rust and Swift

Unlike the other languages that take inspiration from , the enums in and are sum types (see, for example, sum types in Haskell):

22254 questions
195
votes
9 answers

Serialising an Enum member to JSON

How do I serialise a Python Enum member to JSON, so that I can deserialise the resulting JSON back into a Python object? For example, this code: from enum import Enum import json class Status(Enum): success =…
Bilal Syed Hussain
  • 8,664
  • 11
  • 38
  • 44
189
votes
11 answers

Getting the max value of an enum

How do you get the max value of an enum?
jdelator
  • 4,101
  • 6
  • 39
  • 53
188
votes
18 answers

What's the advantage of a Java enum versus a class with public static final fields?

I am very familiar with C# but starting to work more in Java. I expected to learn that enums in Java were basically equivalent to those in C# but apparently this is not the case. Initially I was excited to learn that Java enums could contain…
Craig W
  • 4,390
  • 5
  • 33
  • 51
186
votes
22 answers

Why Python 3.6.1 throws AttributeError: module 'enum' has no attribute 'IntFlag'?

I just installed Python 3.6.1 for MacOS X When I attempt to run the Console(or run anything with Python3), this error is thrown: AttributeError: module 'enum' has no attribute 'IntFlag' $…
BryanWheelock
  • 12,146
  • 18
  • 64
  • 109
185
votes
3 answers

UML class diagram enum

I am modeling a class diagram. An attribute of a class is an enumeration. How do I model this? Normally you do something like this: - name : string But how does one do this with an enum?
Martijn
  • 24,441
  • 60
  • 174
  • 261
185
votes
13 answers

How to get enum value by string or int

How can I get the enum value if I have the enum string or enum int value. eg: If i have an enum as follows: public enum TestEnum { Value1 = 1, Value2 = 2, Value3 = 3 } and in some string variable I have the value "value1" as…
Abhishek Gahlout
  • 3,132
  • 2
  • 20
  • 28
182
votes
17 answers

convert an enum to another type of enum

I have an enum of for example 'Gender' (Male =0 , Female =1) and I have another enum from a service which has its own Gender enum (Male =0 , Female =1, Unknown =2) My question is how can I write something quick and nice to convert from their enum…
kurasa
  • 5,205
  • 9
  • 38
  • 53
180
votes
6 answers

Why was "Avoid Enums Where You Only Need Ints" removed from Android's performance tips?

The section "Avoid Enums Where You Only Need Ints" was removed from the official developer documentation. (See Why doesn't Android use more enums? for the old section content) Why? Was there a change in the Android VM that made the tip obsolete?
Thierry Roy
  • 8,452
  • 10
  • 60
  • 84
179
votes
8 answers

Java Enum definition

I thought I understood Java generics pretty well, but then I came across the following in java.lang.Enum: class Enum> Could someone explain how to interpret this type parameter? Bonus points for providing other examples of where a…
Dónal
  • 185,044
  • 174
  • 569
  • 824
177
votes
9 answers

Is it possible to use Swift's Enum in Obj-C?

I'm trying to convert some of my Obj-C class to Swift. And some other Obj-C classes still using enum in that converted class. I searched In the Pre-Release Docs and couldn't find it or maybe I missed it. Is there a way to use Swift enum in Obj-C…
myLifeasdog
  • 1,959
  • 2
  • 13
  • 11
176
votes
5 answers

Pass enums in angular2 view templates

Can we use enums in an angular2 view template? passes the string as input: enum DropdownType { instrument, account, currency } @Component({ selector: '[.Dropdown]', }) export…
McLac
  • 2,713
  • 3
  • 15
  • 19
175
votes
8 answers

How to add extension methods to Enums

I have this Enum code: enum Duration { Day, Week, Month }; Can I add a extension methods for this Enum?
user2110292
  • 3,637
  • 7
  • 22
  • 22
174
votes
8 answers

Why shouldn't Java enum literals be able to have generic type parameters?

Java enums are great. So are generics. Of course we all know the limitations of the latter because of type erasure. But there is one thing I don't understand, Why can't I create an enum like this: public enum MyEnum { LITERAL1, …
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
173
votes
5 answers

Declaring an enum within a class

In the following code snippet, the Color enum is declared within the Car class in order to limit the scope of the enum and to try not to "pollute" the global namespace. class Car { public: enum Color { RED, BLUE, WHITE …
bporter
  • 3,572
  • 4
  • 24
  • 23
172
votes
2 answers

Get enumeration name by value

Are there any standard methods to get Enumeration names by value? An example: class Example(enum.Enum): one = 1 two = 2 ex_variable = 1 Given ex_variable, can I obtain the string contained in Example.one.name?
Jiloc
  • 3,338
  • 3
  • 24
  • 38