Enums

From the PHP documentation:

Enums allow a developer to define a custom type that is limited to one of a discrete number of possible values.

THP enums are a 1 to 1 map of PHP enums, with a slightly different syntax.

Basic enums

Enums don’t have a scalar value by default.


enum Suit {
╰╴No statement matched
Hearts, Diamonds, Clubs, Spades, } val suit = Suit::Hearts
thp

Backed enums

Backed enums can have a scalar for each case. The scalar values can only be String or Int.


enum Suit(String) {
╰╴No statement matched
Hearts = "H", Diamonds = "D", Clubs = "C", Spades = "S", }
thp

All cases must explicitly define their value, there is no automatic generation.