|
| 1 | +- # Enum |
| 2 | + - Also known as: enumeration, enumerated type |
| 3 | + - A distinct data type that consists of a set of named constants, representing a collection of related values |
| 4 | + - Each named constant is called a `variant` (in some languages) or `enumerator` |
| 5 | + - ## Key Benefits |
| 6 | + - ### Improved Readability and Maintainability |
| 7 | + - Using descriptive names instead of arbitrary numbers or strings makes code more self-explanatory |
| 8 | + - Example: `Color.RED` is clearer than using `0` to represent a color |
| 9 | + - ### Type Safety |
| 10 | + - Restricts variables to predefined values, reducing the risk of invalid assignments |
| 11 | + - Ensures only valid, expected values are used, preventing bugs |
| 12 | + - ### Encapsulation |
| 13 | + - Groups related constants within a single data type |
| 14 | + - Organizes code more effectively and reduces errors from using unrelated constants |
| 15 | + - ## Common Use Cases |
| 16 | + - **State Management**: Representing discrete states (e.g., `PENDING`, `IN_PROGRESS`, `COMPLETED`) |
| 17 | + - **Configuration Options**: Defining sets of options or modes (e.g., logging levels: `DEBUG`, `INFO`, `WARNING`, `ERROR`) |
| 18 | + - **Control Flow**: Using enums in `switch` or `case` statements to handle different scenarios |
| 19 | + - **Error Handling**: Representing success/failure states (e.g., `Result<T, E>` in Rust) |
| 20 | + - **Nullable Values**: Representing optional values (e.g., `Option<T>` in Rust, `Optional<T>` in Java) |
| 21 | + - ## Language Implementations |
| 22 | + - ### C |
| 23 | + - Declared using the `enum` keyword |
| 24 | + - Creates a set of named integer constants |
| 25 | + - By default, values start at 0 and increment |
| 26 | + - ~~~c |
| 27 | + enum Color { RED, GREEN, BLUE }; |
| 28 | + // RED = 0, GREEN = 1, BLUE = 2 |
| 29 | + ~~~ |
| 30 | + - ### C++ |
| 31 | + - Supports unscoped enums (similar to C) |
| 32 | + - C++11 introduced scoped enums with `enum class` for better type safety |
| 33 | + - Prevents implicit conversions |
| 34 | + - ~~~cpp |
| 35 | + enum class Color { RED, GREEN, BLUE }; |
| 36 | + // Access: Color::RED |
| 37 | + ~~~ |
| 38 | + - ### Java |
| 39 | + - More robust enums that can have methods and fields |
| 40 | + - Can implement interfaces |
| 41 | + - Each enumerator is an instance of the enum type |
| 42 | + - ~~~java |
| 43 | + public enum Day { |
| 44 | + SUNDAY, MONDAY, TUESDAY, WEDNESDAY, |
| 45 | + THURSDAY, FRIDAY, SATURDAY; |
| 46 | + } |
| 47 | + ~~~ |
| 48 | + - ### Python |
| 49 | + - Provided by the `enum` module |
| 50 | + - Enumerators can have custom values |
| 51 | + - Supports iteration |
| 52 | + - ~~~python |
| 53 | + from enum import Enum |
| 54 | + |
| 55 | + class Color(Enum): |
| 56 | + RED = 1 |
| 57 | + GREEN = 2 |
| 58 | + BLUE = 3 |
| 59 | + ~~~ |
| 60 | + - ### Rust |
| 61 | + - Powerful enums that can hold data (sum types / tagged unions) |
| 62 | + - Each variant can have associated data |
| 63 | + - Used extensively for error handling and optional values |
| 64 | + - ~~~rust |
| 65 | + enum Result<T, E> { |
| 66 | + Ok(T), |
| 67 | + Err(E), |
| 68 | + } |
| 69 | + ~~~ |
| 70 | + - ## Considerations |
| 71 | + - ### Serialization |
| 72 | + - When storing or transmitting enum values, ensure consistent representation (integer, string, etc.) |
| 73 | + - All parts of the system must understand the chosen representation |
| 74 | + - ### Extensibility |
| 75 | + - Adding new values can affect existing code |
| 76 | + - `switch` or `match` statements without default cases may need updates |
| 77 | + - Plan for potential extensions to avoid maintenance challenges |
| 78 | + - ## Related |
| 79 | + - [[Rust/std/result/Result]] - Example of a powerful enum in Rust |
| 80 | + - [[Programming/Language/Feature]] - Language features in general |
| 81 | + - Type safety and type systems |
| 82 | + - Sum types / Tagged unions (in functional programming languages) |
| 83 | + |
0 commit comments