Created
January 24, 2021 15:26
-
-
Save Sija/247c56da87e89b6922bf2114bc6c35e7 to your computer and use it in GitHub Desktop.
Originally posted by @asterite in https://github.com/crystal-lang/crystal/issues/10281#issuecomment-766358669
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro string_enum(name, &block) | |
enum {{name}} | |
{% for exp in block.body.expressions %} | |
{% if exp.is_a?(Assign) %} | |
{{exp.target}} | |
{% else %} | |
{{exp}} | |
{% end %} | |
{% end %} | |
def to_s | |
case self | |
{% for exp in block.body.expressions %} | |
{% if exp.is_a?(Assign) %} | |
in {{exp.target}} | |
{{exp.value}} | |
{% end %} | |
{% end %} | |
end | |
end | |
end | |
end | |
string_enum Numbers do | |
One = "1" | |
Two = "2" | |
Three = "3" | |
def hello | |
"Hello!" | |
end | |
end | |
puts Numbers::One | |
puts Numbers::Two | |
puts Numbers::Three | |
puts Numbers::One.hello |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment