WHAT ARE ENUMS AND WHY TO USE THEM IN SOLIDITY?

10 April 2022, Abdulhakim Altunkaya

In solidity documentation, Enums are described as "one-way" to describe user-defined data types in Solidity. Enums are useful to keep track of our choices. Imagine we can choose only one hamburger from a list of ten hamburgers. After choosing our hamburger, we can store our choice in a variable (which will be an enum variable). We can change our hamburger choice, which in other words means we can modify our Enum variable state. You may be hungry and would like to choose two burgers, but we cannot do that with Enums because an Enum variable can store only one burger. Therefore, you will need to make another order, and save your choice in a second Enum variable.

Now technical side, here is a small summary:

1. Enum variables have their values in enum format. But Solidity converts this enum values to uint8 for our convenience. However, this is not a total conversion. We will see uint8 when we return a Enum variable but it will not act like a uint8 when we want to do a math operation. For example we cannot add/subtract/.. etc an Enum value. To make math operation on an Enum value, we need to convert it to uint by typecasting such as: uint(...our Enum value...)

2. You cannot use public or other tags on Enum lists.

3. But you can use public or other tags on Enum variables.

4. You cannot iterate an Enum list: no for loop, no while loop, no ".length"

5. You cannot store address, bool, uint, int or string in an Enum list.

6. You cannot store Enum values in a uint array.

7. But you can store Enum values in an Enum array.

8. An Enum variable can have only one Enum value.

9. An Enum list needs to have at least one value.

10. Enum lists are not Arrays. In arrays, you can add to an array or remove a value from an array. But you can neither add to an Enum list nor remove anything from it. If you want to add or remove an Enum value in an Enum list, then you need to re-write your Enum list.

11. Enum lists can be declared outside of a contract.

//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.7;

contract EnumExample {

//Here we create our Enum list with 4 Enum values
enum Hamburgers {CHILDREN, SIZE1, SIZE2, SIZE3}

//We create an Enum variable.
Hamburgers public myChoice;

//First way: set an Enum value for our Enum variable
function setChoice() external {
myChoice = Hamburgers(3);
}

//Second way: the same function above
function setChoice2() external {
myChoice = Hamburgers.SIZE3;
}

//Returning Enum variable
function getEnum1() external view returns(Hamburgers) {
return myChoice;
}

//Returning an Enum value. We are using pure because Enum lists are not changing the state.
function getEnum2() external pure returns(Hamburgers) {
return Hamburgers(2);
}

//Returning an Enum value (same as above, written differently)
function getEnum3() external pure returns(Hamburgers) {
return Hamburgers.SIZE2;
}

//Return and make a math operation on Enum value.
We need to typecast the Enum value.

function getEnum4() external pure returns(uint8) {
return uint8(Hamburgers.SIZE2) + 3;
}

//Create an Enum array named "myBurgers"
Hamburgers[] myBurgers = [Hamburgers(2), Hamburgers(1)];
}

For more information on Enums, you can check the links below:

Educative.io article by Shubham Singh Kshatriya

Solidity documentation. However, this page contains tons of other information. So CTRL+F and search "enum".

Tutorials.point article about Enums

A great Medium article about Enums by Jean Cvllr