Get enum underlying type in compile time

Is it possible to get enum underlying type in compile type (i.e. in macro)? Stuck with this trying to support enum types in storage schema providing correct storage type like UInt8/UInt16 so to reduce storage usage but also support large enums if needed

TypeNode doesn’t support it but you could try one of the following:

enum Foo : Int8
  X
end

# `NumberLiteral#kind : SymbolLiteral`
{{ Foo.constant(Foo.constants.first).kind }} # => :i8
# `EnumDef#base_type : Path`
macro foo(x)
  {{ x.base_type }} # => Int8
end

foo enum Foo : Int8
  X
end
1 Like

Related: Document how to change base type of an enum by Blacksmoke16 · Pull Request #9803 · crystal-lang/crystal · GitHub. Was a PR to add this, but wasn’t a valid use case to warrant the addition at the time.