2.8.4 Enum abstracts

since Haxe 3.1.0

By adding the :enum metadata to an abstract definition, that abstract can be used to define finite value sets:

abstract HttpStatus(Int) {
  var NotFound = 404;
  var MethodNotAllowed = 405;

class Main {
  static public function main() {
    var status = HttpStatus.NotFound;
    var msg = printStatus(status);

  static function printStatus(status:HttpStatus) {
    return switch(status) {
      case NotFound:
        "Not found";
      case MethodNotAllowed:
        "Method not allowed";

The Haxe Compiler replaces all field access to the HttpStatus abstract with their values, as evident in the JavaScript output:

Main.main = function() {
    var status = 404;
    var msg = Main.printStatus(status);
Main.printStatus = function(status) {
    switch(status) {
    case 404:
        return "Not found";
    case 405:
        return "Method not allowed";

This is similar to accessing variables declared as inline, but has several advantages:

  • The typer can ensure that all values of the set are typed correctly.
  • The pattern matcher checks for exhaustiveness when matching an enum abstract.
  • Defining fields requires less syntax.