Hey everyone, today we’re giving a short introduction to Enums in Haxe and how you can use them to improve your code readability, clarity, and safety. If you’ve come from languages like Java or the .NET family, they should not be a foreign concept to you, but stick around, because there are some quirks in Haxe. Let’s give a brief intro to Enums.

Enums In Haxe

Enumerations or Enums for short are names given to constants that allow them to be used as special identifiers in a program. You can’t change an enum or modify what the value represents once you create it. They are very useful for giving types of data in your program clear differences while still being lightweight. For example, I may write an Enum in a game to determine the directions the player and enemy can face. In Haxe it would look something like the below.

enum Direction {
function main() {
function currentDirection(direction:Direction) {
switch (direction) {
case Left:
trace('facing left');
case Right:
trace('facing right');
case Up:
trace('facing up');
case Down:
trace('facing down');

With this Enum in place, we simplify checking the direction of enemies and players, improve readability, and create constants that can be used across the engine. This is an Enum in Haxe at its most basic (We’ll have a follow-up post on creating more interesting and complex ones). There’s also a thin set of tools that can be used in Haxe to create enums from strings and more. There’s a lot you can do with them and I use them personally in my own code to separate important data types and establish dependencies.

But really, what’s the point of using them anyway? Let’s get into how you could use them in your own code with concrete advantages.

Enum Advantages

The advantage of an enum is code clarity. It also allows for a more concrete separation of data types. You can’t use any other data to represent a direction besides the ones you created for that enum(Left, Right, Up, Down). That means you can be even lazier when you write your own code and who doesn’t want that? Now you might think, you could just use a basic data type like a string or an integer, but you run into problems later.

Someone might pass in the wrong data into your system; the function you wrote takes an integer when in reality that integer is supposed to be one of your “enums”. Now, that person is confused because they have no idea why the function doesn’t work; that person might be you. By making a real enumerator and creating that separation in your code will prevent similar problems from cropping up, and make your codebase a safer place to work in. That’s very powerful and allows you to feel more secure when creating more complex applications in Haxe. The important thing is that it allows you to leverage the type system to improve your code.

That’s all there is for this basic introduction to enums. Next time we’ll get into more complex enums.

With that said, I hope this helps and stay tuned for more posts in the future on enums!

%d bloggers like this: