Created
June 10, 2020 04:59
-
-
Save tonogram/efabe3a222cd34c5a1a8e909b7ff6a51 to your computer and use it in GitHub Desktop.
Get name of Nim enum with attached string
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
from macros import toStrLit | |
type myEnum = enum | |
Example = "Hello, world!" | |
macro getName(x: enum): string = | |
x.toStrLit() | |
assert $Example == "Hello, world!" # $ (toString) gets attached string of enum | |
assert Example.getName() == "Example" # getName gets name of enum | |
assert Example.ord() == 0 # ord() gets ordinal value of enum [int() also works] | |
# Solution by Tongir on Nim's IRC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment