Skip to content

Instantly share code, notes, and snippets.

@tonogram
Created June 10, 2020 04:59
Show Gist options
  • Save tonogram/efabe3a222cd34c5a1a8e909b7ff6a51 to your computer and use it in GitHub Desktop.
Save tonogram/efabe3a222cd34c5a1a8e909b7ff6a51 to your computer and use it in GitHub Desktop.
Get name of Nim enum with attached string
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