Source Edit

Imports

macros, typetraits

Procs

  1. func symbolName[T: enum](a: T): string

Returns the symbol name of an enum.

This uses symbolRank.

Example:

  1. type B = enum
  2. b0 = (10, "kb0")
  3. b1 = "kb1"
  4. b2
  5. let b = B.low
  6. assert b.symbolName == "b0"
  7. assert $b == "kb0"
  8. static: assert B.high.symbolName == "b2"
  9. type C = enum # HoleyEnum
  10. c0 = -3
  11. c1 = 4
  12. c2 = 20
  13. assert c1.symbolName == "c1"

Source Edit

Iterators

  1. iterator items[T: HoleyEnum](E: typedesc[T]): T

Iterates over an enum with holes.

Example:

  1. type
  2. A = enum
  3. a0 = 2
  4. a1 = 4
  5. a2
  6. B[T] = enum
  7. b0 = 2
  8. b1 = 4
  9. from std/sequtils import toSeq
  10. assert A.toSeq == [a0, a1, a2]
  11. assert B[float].toSeq == [B[float].b0, B[float].b1]

Source Edit

Macros

  1. macro genEnumCaseStmt(typ: typedesc; argSym: typed; default: typed;
  2. userMin, userMax: static[int];
  3. normalizer: static[proc (s: string): string]): untyped

Source Edit

Templates

  1. template symbolRank[T: enum](a: T): int

Returns the index in which a is listed in T.

The cost for a HoleyEnum is implementation defined, currently optimized for small enums, otherwise is O(T.enumLen).

Example:

  1. type
  2. A = enum # HoleyEnum
  3. a0 = -3
  4. a1 = 10
  5. a2
  6. a3 = (20, "f3Alt")
  7. B = enum # OrdinalEnum
  8. b0
  9. b1
  10. b2
  11. C = enum # OrdinalEnum
  12. c0 = 10
  13. c1
  14. c2
  15. assert a2.symbolRank == 2
  16. assert b2.symbolRank == 2
  17. assert c2.symbolRank == 2
  18. assert c2.ord == 12
  19. assert a2.ord == 11
  20. var invalid = 7.A
  21. doAssertRaises(IndexDefect): discard invalid.symbolRank

Source Edit