Implementation: src/hql/transpiler/syntax/enum.ts (parsing), src/hql/transpiler/pipeline/ir-to-typescript.ts (codegen)
Enumerations in HQL provide groups of named constants. HQL supports three types of enums:
There is also a TypeScript-specific const-enum form.
(enum Direction
(case north)
(case south)
(case east)
(case west))
Direction.north ;; => "north"
Compiles to:
const Direction = Object.freeze({
north: "north",
south: "south",
east: "east",
west: "west"
});
(enum HttpStatus
(case ok 200)
(case notFound 404)
(case serverError 500))
HttpStatus.notFound ;; => 404
Raw values can be any literal (number or string). Access is direct property access on the frozen object.
An optional raw type can be specified either embedded with a colon or as a separate token:
;; Colon syntax
(enum HttpStatus:Int
(case ok 200)
(case notFound 404))
;; Separate token syntax
(enum HttpStatus Int
(case ok 200)
(case notFound 404))
The raw type is stored in the IR but does not affect JavaScript code generation.
(enum Payment
(case cash amount)
(case creditCard number expiry))
;; Create instance via static factory method
(var payment (Payment.cash 100))
;; Check type
(payment.is "cash") ;; => true
;; Access associated values via the .values property
(get payment.values "amount") ;; => 100
Compiles to a class with:
type and values instance propertiesObject.freeze(this)is(type) instance method (returns this.type === type)Payment.cash(amount))(const-enum Direction [North South East West])
;; With explicit values
(const-enum Status [(OK 200) (NotFound 404) (Error 500)])
;; With string values
(const-enum Color [(Red "red") (Green "green") (Blue "blue")])
Compiles to TypeScript const enum declarations. This is a separate form from (enum ...).
The syntax transformer resolves .caseName shorthand to EnumName.caseName by searching known enum definitions for a matching case:
(enum OS
(case macOS)
(case linux))
;; .macOS is resolved to OS.macOS if OS is the only enum with a macOS case
(=== os .macOS)
This resolution happens at the syntax transformer stage before IR generation.
===cond expressions>=).is() method(get payment.values "key")EnumName.caseName in function parameters and equality checks