Web Development With HHVM and Hack 11: Enums

Mike Abelar
3 min readApr 3, 2020

--

In the last tutorial, we covered shapes in Hack: https://medium.com/@mikeabelar/web-development-with-hhvm-and-hack-10-shapes-d8a56b067c2b. In this tutorial, we will cover enums.

What are enums?

Enums allow us to represent a list of choices that all have the same underlying data type. Take the following example: say we would like to represent the 4 directions in code. We would want to do this because our application will need to do many if statements on the current direction to see if it is north, south, east or west. To make things worse, we want to do represent each direction as an integer such that 1 represents North, 2 represents South, 3 represents East and 4 represents West. In our code, we will have to do many checks on the current direction:

// set the direction to north
$current_direction = 1;
if ($current_direction == 1) {
print("North!");
} elseif ($current_direction == 2) {
print("South!");
} elseif ($current_direction == 3) {
print("East!");
} elseif ($current_direction == 4) {
print("West!");
}

Continually using integers to represent directions can get confusing quickly. If you see the number 3 in code, you have to recall that the code means East. To make code more readable, we introduce the enum. The enum represents abstract concepts as underlying integers or strings.

Enum Example

Let us create an enum for the situation above. First, create a folder for this tutorial and in that folder run: touch .hhconfig to start our project. Then, let’s create a file: touch enums.hack . Paste the following contents:

enum Direction: int {
North = 1;
South = 2;
East = 3;
West = 4;
}
<<__EntryPoint>>
function main() : noreturn {
print("Every value of the enum:\n");
// this variable is a vec
$enum_names = Direction::getNames();
foreach ($enum_names as $enum_name) {
print($enum_name . "\n");
}
exit(0);
}

In this code, we define an enum, Direction which matches our problem that we started the tutorial with. To declare an enum, use the enum keyword followed by the name of the enum followed by a colon and the type of the enum. Note: the type can only be either an int or a string. In the code, we then print each possible value of the enum. We get all the enum values by calling Direction::getNames() . For your enum, replace Direction with the name of your enum.

Switch Statements

Before in the tutorial, I used an if statement to compare every possible value of current_direction . An alternative way to compare values, especially enums, is to use a switch statement. Here if the code for comparing the value of the enum, but using switch statements:

enum Direction: int {
North = 1;
South = 2;
East = 3;
West = 4;
}
<<__EntryPoint>>
function main() : noreturn {
// set current_direction to be north
$current_direction = Direction::North;
switch ($current_direction) {
case Direction::North:
print("North!");
break;
case Direction::South:
print("South!");
break;
case Direction::East:
print("East!");
break;
case Direction::West:
print("West!");
break;
}
exit(0);
}

To start a switch statement, use the switch keyword, taking in the variable you want to switch on. Then, for each potential value, use the case keyword, followed by the value, which in this case is the various values of the enum. The value of an enum can be accessed by the name of the enum followed by the name of the particular choice of the enum. So Direction::North we will return 1. Then, put the code after the case statement. Once you put the code, put a break statement, indicating that the case has ended.

In the next tutorial, we cover classes: https://medium.com/@mikeabelar/web-development-with-hhvm-and-hack-12-classes-part-1-b858d1b61cbc

--

--