Skip to content

Why only integral values in enums? #126

Answered by edubart
ghost asked this question in Q&A
Discussion options

You must be logged in to vote

While making an enum with strings officially is not planned, this doable with meta programming, and here I will share how.

1. Naive enum

First you can use a simple naive enum of strings using global record fields, like this:

-- Define an enum of strings.
global MyStringEnum: type = @record{}
global MyStringEnum.A: string = 'A'
global MyStringEnum.B: string = 'B'

-- Example of use
print(MyStringEnum.A)
print(MyStringEnum.B)

Like you can see, we have to declare meta fields in the record for each enum field.

2. Macro string enum

The naive enum works, but can we compress the syntax by generating the enum through the preprocessor? Turns out we can:

-- Define macro that will create an enum
## l…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ghost
Comment options

Answer selected by edubart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant