Reflaxe.Elixir provides elixir.types.Atom as a type-safe, zero-cost way to represent Elixir atoms in Haxe.
Atomis anabstract Atom(String)that compiles to an Elixir atom (e.g.:ok), not a string.- Use it to make intent explicit and prevent accidentally passing
"ok"where an atom is required.
Reference implementation: std/elixir/types/Atom.hx.
- Elixir APIs that accept atoms for options:
:temporary_assigns,:compress, etc. - Result/status atoms:
:ok,:error,:noreply,:stop. - Enum-like option sets, best modeled as
enum abstractoverAtom.
import elixir.types.Atom;
var ok: Atom = "ok"; // generates :ok
var error: Atom = "error"; // generates :errorCompiles to:
ok = :ok
error = :errorPrefer an enum abstract over Atom when the API takes a fixed set of atoms.
import elixir.types.Atom;
enum abstract TimeUnit(Atom) to Atom {
var Second = "second";
var Millisecond = "millisecond";
var Microsecond = "microsecond";
}Compiles to atom values consumed by Elixir APIs, for example:
:second
:millisecond
:microsecond- Atoms are not garbage-collected on the BEAM.
- Do not create atoms from untrusted or unbounded user input.
- Prefer strings for user-provided keys and values, and only use
Atomfor known, finite sets.