summary refs log tree commit diff stats
path: root/lib/std
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2021-02-23 11:25:35 -0800
committerGitHub <noreply@github.com>2021-02-23 20:25:35 +0100
commitc274e67198e446f07708c0a14751da3b2832cec9 (patch)
tree2fb9dcbe5b56f8f71a55af2856033e38565e76e2 /lib/std
parent74a8f23801316cd9e5b7ea072bf0c8c0465b0626 (diff)
downloadNim-c274e67198e446f07708c0a14751da3b2832cec9.tar.gz
add enumutils.items for sparse enums, typetraits.SomeSparseEnum (#17080)
* add enumutils.items for enum with holes
* changelog
* ref in lib.rst
* use `type SomeSparseEnum* = (not Ordinal) and enum` instead of concept
* address comment: rename back to enum with holes
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/enumutils.nim19
1 files changed, 17 insertions, 2 deletions
diff --git a/lib/std/enumutils.nim b/lib/std/enumutils.nim
index 704e42de5..56a6d82a7 100644
--- a/lib/std/enumutils.nim
+++ b/lib/std/enumutils.nim
@@ -7,7 +7,9 @@
 #    distribution, for details about the copyright.
 #
 
-import macros
+import std/macros
+
+# xxx `genEnumCaseStmt` needs tests and runnableExamples
 
 macro genEnumCaseStmt*(typ: typedesc, argSym: typed, default: typed, 
             userMin, userMax: static[int], normalizer: static[proc(s :string): string]): untyped =
@@ -61,4 +63,17 @@ macro genEnumCaseStmt*(typ: typedesc, argSym: typed, default: typed,
     result.add nnkElse.newTree(raiseStmt)
   else:
     expectKind(default, nnkSym)
-    result.add nnkElse.newTree(default)
\ No newline at end of file
+    result.add nnkElse.newTree(default)
+
+macro enumWithHolesFullRange(a: typed): untyped = 
+  newNimNode(nnkCurly).add(a.getType[1][1..^1])
+
+iterator items*[T: enum and not Ordinal](E: typedesc[T]): T =
+  ## Iterates over an enum with holes.
+  runnableExamples:
+    type A = enum a0 = 2, a1 = 4, a2
+    type B[T] = enum b0 = 2, b1 = 4
+    from std/sequtils import toSeq
+    assert A.toSeq == [a0, a1, a2]
+    assert B[float].toSeq == [B[float].b0, B[float].b1]
+  for a in enumWithHolesFullRange(E): yield a