EnumAbstractMacro

ceramic.macros.EnumAbstractMacro (Class)

Utility macros for working with enum abstracts at compile time. Enum abstracts are Haxe's way of creating type-safe enumerations with custom underlying types. This macro provides tools to:

  • Get all values of an enum abstract as an array
  • Convert enum abstract values to/from strings

These utilities are particularly useful for serialization, UI generation, and validation where you need to work with all possible enum values.

Example usage:

var allStates = EnumAbstractMacro.getValues(MyState);
var stateStr = EnumAbstractMacro.toStringSwitch(MyState, currentState);
var state = EnumAbstractMacro.fromStringSwitch(MyState, "IDLE");

Static Members

getValues(typePath: Dynamic): Dynamic

Returns an array containing all values of an enum abstract. This macro examines the enum abstract at compile time and generates code that creates an array of all its values.

Example:

 abstract Color(Int) {
    var RED = 0xFF0000;
    var GREEN = 0x00FF00;
    var BLUE = 0x0000FF;
}

var colors = EnumAbstractMacro.getValues(Color); // [RED, GREEN, BLUE]
Name Type Description
typePath Dynamic Expression representing the enum abstract type
Returns Description
Dynamic Expression that evaluates to an array of all enum values

toStringSwitch(typePath: Dynamic, e: Dynamic): Dynamic

Generates a switch expression that converts an enum abstract value to its string name. Creates an exhaustive switch that maps each enum value to its identifier as a string.

Example:

var state = PLAYING;
var name = EnumAbstractMacro.toStringSwitch(GameState, state); // "PLAYING"

This is useful for serialization, debugging, and displaying enum values in UIs.

Name Type Description
typePath Dynamic Expression representing the enum abstract type
e Dynamic Expression of the enum value to convert
Returns Description
Dynamic Switch expression that evaluates to the string name

fromStringSwitch(typePath: Dynamic, e: Dynamic): Dynamic

Generates a switch expression that converts a string to an enum abstract value. Creates an exhaustive switch that maps string names to their corresponding enum values. Throws an exception if the string doesn't match any enum value.

Example:

var state = EnumAbstractMacro.fromStringSwitch(GameState, "PLAYING"); // GameState.PLAYING
// Throws: Cannot convert "INVALID" to GameState

This is the inverse of toStringSwitch and is useful for deserialization and parsing user input.

Name Type Description
typePath Dynamic Expression representing the enum abstract type
e Dynamic Expression of the string to convert
Returns Description
Dynamic Block expression that evaluates to the enum value or throws