From c3f638d4a5e89f9f83fa4c9faff3d0d537735d6a Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 17 Feb 2021 16:56:22 -0800 Subject: [PATCH 01/14] add enumutils.items for enum with holes --- lib/pure/typetraits.nim | 16 ++++++++++++++++ lib/std/enumutils.nim | 13 +++++++++++-- lib/system/iterators.nim | 9 +++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim index 7af78bf31f887..f2b6aa21eb9ae 100644 --- a/lib/pure/typetraits.nim +++ b/lib/pure/typetraits.nim @@ -15,6 +15,22 @@ import std/private/since export system.`$` # for backward compatibility +type SomeHolyEnum* = concept x ## Type class for enum's with holes. + x is enum and x isnot Ordinal + +#[ +xxx `runnableExamples` isn't run if inside: +type SomeHolyEnum* = concept x + runnableExamples: assert false + x is enum and x isnot Ordinal +]# + +runnableExamples: + type Goo = enum g0 = 2, g1, g2 + type Hoo = enum h0 = 2, h1 = 4, h2 + assert Goo isnot SomeHolyEnum + assert Hoo is SomeHolyEnum + proc name*(t: typedesc): string {.magic: "TypeTrait".} = ## Returns the name of the given type. ## diff --git a/lib/std/enumutils.nim b/lib/std/enumutils.nim index 704e42de5227e..c78c6d0d9aeaa 100644 --- a/lib/std/enumutils.nim +++ b/lib/std/enumutils.nim @@ -7,7 +7,7 @@ # distribution, for details about the copyright. # -import macros +import std/macros macro genEnumCaseStmt*(typ: typedesc, argSym: typed, default: typed, userMin, userMax: static[int], normalizer: static[proc(s :string): string]): untyped = @@ -61,4 +61,13 @@ 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 holyEnumFullRange(a: typed): untyped = result = newNimNode(nnkCurly).add(a.getType[1][1..^1]) + +iterator items*[T: enum and not Ordinal](E: typedesc[T]): T = + runnableExamples: + type Hoo = enum h0 = 2, h1 = 4, h2 + from sequtils import toSeq + assert Hoo.toSeq == [h0, h1, h2] + for a in holyEnumFullRange(E): yield a diff --git a/lib/system/iterators.nim b/lib/system/iterators.nim index 504695bf6296e..7f0076a444652 100644 --- a/lib/system/iterators.nim +++ b/lib/system/iterators.nim @@ -82,8 +82,13 @@ iterator mitems*(a: var cstring): var char {.inline.} = yield a[i] inc(i) -iterator items*[T: enum](E: typedesc[T]): T = - ## Iterates over the values of the enum ``E``. +iterator items*[T: enum and Ordinal](E: typedesc[T]): T = + ## Iterates over the values of `E`. + ## See also `enumutils.items` for enums with holes. + runnableExamples: + type Goo = enum g0 = 2, g1, g2 + from std/sequtils import toSeq + assert Goo.toSeq == [g0, g1, g2] for v in low(E) .. high(E): yield v From d384acd2511cbe5aedaa23b183b49c686e79d87b Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 17 Feb 2021 16:58:05 -0800 Subject: [PATCH 02/14] changelog --- changelog.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 8caee8affc750..8351dd981309b 100644 --- a/changelog.md +++ b/changelog.md @@ -41,9 +41,13 @@ - Added `randState` template that exposes the default random number generator. Useful for library authors. -- Added std/enumutils module containing `genEnumCaseStmt` macro that generates +- Added `std/enumutils` module containing `genEnumCaseStmt` macro that generates case statement to parse string to enum. +- Added `enumutils.items` for enums with holes + +- Added `typetraits.SomeHolyEnum` for enums with holes + - Removed deprecated `iup` module from stdlib, it has already moved to [nimble](https://github.com/nim-lang/iup). From d68261e14a12f8da4f3b6edf4dd07d3c5093ac02 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 17 Feb 2021 17:05:35 -0800 Subject: [PATCH 03/14] fixup --- lib/pure/typetraits.nim | 11 +++++++---- lib/std/enumutils.nim | 6 ++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim index f2b6aa21eb9ae..c549a949063c4 100644 --- a/lib/pure/typetraits.nim +++ b/lib/pure/typetraits.nim @@ -26,10 +26,13 @@ type SomeHolyEnum* = concept x ]# runnableExamples: - type Goo = enum g0 = 2, g1, g2 - type Hoo = enum h0 = 2, h1 = 4, h2 - assert Goo isnot SomeHolyEnum - assert Hoo is SomeHolyEnum + type A = enum a0 = 2, a1 = 4, a2 + type B = enum b0 = 2, b1, b2 + assert A is SomeHolyEnum + assert B isnot SomeHolyEnum + assert int isnot SomeHolyEnum + type C[T] = enum h0 = 2, h1 = 4 + assert C[float] is SomeHolyEnum proc name*(t: typedesc): string {.magic: "TypeTrait".} = ## Returns the name of the given type. diff --git a/lib/std/enumutils.nim b/lib/std/enumutils.nim index c78c6d0d9aeaa..3ba6c1ee106dd 100644 --- a/lib/std/enumutils.nim +++ b/lib/std/enumutils.nim @@ -67,7 +67,9 @@ macro holyEnumFullRange(a: typed): untyped = result = newNimNode(nnkCurly).add(a iterator items*[T: enum and not Ordinal](E: typedesc[T]): T = runnableExamples: - type Hoo = enum h0 = 2, h1 = 4, h2 + type A = enum a0 = 2, a1 = 4, a2 + type B[T] = enum b0 = 2, b1 = 4 from sequtils import toSeq - assert Hoo.toSeq == [h0, h1, h2] + assert A.toSeq == [a0, a1, a2] + assert B[float].toSeq == [B[float].b0, B[float].b1] for a in holyEnumFullRange(E): yield a From 37a77a31a23be1db37ad1b110704ae35a254ca42 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 17 Feb 2021 17:08:56 -0800 Subject: [PATCH 04/14] fixup --- changelog.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 8351dd981309b..42a8da1a8b7fc 100644 --- a/changelog.md +++ b/changelog.md @@ -41,8 +41,9 @@ - Added `randState` template that exposes the default random number generator. Useful for library authors. -- Added `std/enumutils` module containing `genEnumCaseStmt` macro that generates - case statement to parse string to enum. +- Added `std/enumutils` module. + +- Added `enumutils.genEnumCaseStmt` macro that generates case statement to parse string to enum. - Added `enumutils.items` for enums with holes From 51f34734097239064e647ab498ca47eca75d7abc Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 17 Feb 2021 17:14:02 -0800 Subject: [PATCH 05/14] rename to SomeSparseEnum etc --- changelog.md | 4 ++-- lib/pure/typetraits.nim | 14 +++++++------- lib/std/enumutils.nim | 5 +++-- lib/system/iterators.nim | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/changelog.md b/changelog.md index 42a8da1a8b7fc..55d95d26ac500 100644 --- a/changelog.md +++ b/changelog.md @@ -45,9 +45,9 @@ - Added `enumutils.genEnumCaseStmt` macro that generates case statement to parse string to enum. -- Added `enumutils.items` for enums with holes +- Added `enumutils.items` for sparse enums. -- Added `typetraits.SomeHolyEnum` for enums with holes +- Added `typetraits.SomeSparseEnum` for sparse enums. - Removed deprecated `iup` module from stdlib, it has already moved to [nimble](https://github.com/nim-lang/iup). diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim index c549a949063c4..250b15855a277 100644 --- a/lib/pure/typetraits.nim +++ b/lib/pure/typetraits.nim @@ -15,24 +15,24 @@ import std/private/since export system.`$` # for backward compatibility -type SomeHolyEnum* = concept x ## Type class for enum's with holes. +type SomeSparseEnum* = concept x ## Type class for enum's with holes. x is enum and x isnot Ordinal #[ xxx `runnableExamples` isn't run if inside: -type SomeHolyEnum* = concept x +type Foo* = concept x runnableExamples: assert false - x is enum and x isnot Ordinal + x is int ]# runnableExamples: type A = enum a0 = 2, a1 = 4, a2 type B = enum b0 = 2, b1, b2 - assert A is SomeHolyEnum - assert B isnot SomeHolyEnum - assert int isnot SomeHolyEnum + assert A is SomeSparseEnum + assert B isnot SomeSparseEnum + assert int isnot SomeSparseEnum type C[T] = enum h0 = 2, h1 = 4 - assert C[float] is SomeHolyEnum + assert C[float] is SomeSparseEnum proc name*(t: typedesc): string {.magic: "TypeTrait".} = ## Returns the name of the given type. diff --git a/lib/std/enumutils.nim b/lib/std/enumutils.nim index 3ba6c1ee106dd..f5b963fca2e92 100644 --- a/lib/std/enumutils.nim +++ b/lib/std/enumutils.nim @@ -63,13 +63,14 @@ macro genEnumCaseStmt*(typ: typedesc, argSym: typed, default: typed, expectKind(default, nnkSym) result.add nnkElse.newTree(default) -macro holyEnumFullRange(a: typed): untyped = result = newNimNode(nnkCurly).add(a.getType[1][1..^1]) +macro sparseEnumFullRange(a: typed): untyped = result = newNimNode(nnkCurly).add(a.getType[1][1..^1]) iterator items*[T: enum and not Ordinal](E: typedesc[T]): T = + ## iterates over a sparse enum runnableExamples: type A = enum a0 = 2, a1 = 4, a2 type B[T] = enum b0 = 2, b1 = 4 from sequtils import toSeq assert A.toSeq == [a0, a1, a2] assert B[float].toSeq == [B[float].b0, B[float].b1] - for a in holyEnumFullRange(E): yield a + for a in sparseEnumFullRange(E): yield a diff --git a/lib/system/iterators.nim b/lib/system/iterators.nim index 7f0076a444652..58a1fdb5cd7e2 100644 --- a/lib/system/iterators.nim +++ b/lib/system/iterators.nim @@ -84,7 +84,7 @@ iterator mitems*(a: var cstring): var char {.inline.} = iterator items*[T: enum and Ordinal](E: typedesc[T]): T = ## Iterates over the values of `E`. - ## See also `enumutils.items` for enums with holes. + ## See also `enumutils.items` for sparse enums. runnableExamples: type Goo = enum g0 = 2, g1, g2 from std/sequtils import toSeq From b8e5c82cbcd721ee670bba029fb5c4b7fe00fb92 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 17 Feb 2021 17:14:21 -0800 Subject: [PATCH 06/14] fixup --- lib/pure/typetraits.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim index 250b15855a277..1ef6f78e054d1 100644 --- a/lib/pure/typetraits.nim +++ b/lib/pure/typetraits.nim @@ -15,7 +15,7 @@ import std/private/since export system.`$` # for backward compatibility -type SomeSparseEnum* = concept x ## Type class for enum's with holes. +type SomeSparseEnum* = concept x ## Type class for sparse enum's. x is enum and x isnot Ordinal #[ From 0aa966879e53be95e42df8ce4e93a0407b820e91 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 17 Feb 2021 23:43:55 -0800 Subject: [PATCH 07/14] address comment --- changelog.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/changelog.md b/changelog.md index 55d95d26ac500..41e10d37347f6 100644 --- a/changelog.md +++ b/changelog.md @@ -41,11 +41,8 @@ - Added `randState` template that exposes the default random number generator. Useful for library authors. -- Added `std/enumutils` module. - -- Added `enumutils.genEnumCaseStmt` macro that generates case statement to parse string to enum. - -- Added `enumutils.items` for sparse enums. +- Added `std/enumutils` module. Added `genEnumCaseStmt` macro that generates case statement to parse string to enum. + Added `items` for sparse enums. - Added `typetraits.SomeSparseEnum` for sparse enums. From 8b502c87fd952cdb422848eca046c7e08c8c6564 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 17 Feb 2021 23:48:18 -0800 Subject: [PATCH 08/14] tests --- lib/std/enumutils.nim | 5 ++++- tests/stdlib/tenumutils.nim | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/stdlib/tenumutils.nim diff --git a/lib/std/enumutils.nim b/lib/std/enumutils.nim index f5b963fca2e92..0bdf736e42e13 100644 --- a/lib/std/enumutils.nim +++ b/lib/std/enumutils.nim @@ -9,6 +9,8 @@ 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 = # generates a case stmt, which assigns the correct enum field given @@ -63,7 +65,8 @@ macro genEnumCaseStmt*(typ: typedesc, argSym: typed, default: typed, expectKind(default, nnkSym) result.add nnkElse.newTree(default) -macro sparseEnumFullRange(a: typed): untyped = result = newNimNode(nnkCurly).add(a.getType[1][1..^1]) +macro sparseEnumFullRange(a: typed): untyped = + newNimNode(nnkCurly).add(a.getType[1][1..^1]) iterator items*[T: enum and not Ordinal](E: typedesc[T]): T = ## iterates over a sparse enum diff --git a/tests/stdlib/tenumutils.nim b/tests/stdlib/tenumutils.nim new file mode 100644 index 0000000000000..6dbcb4308144b --- /dev/null +++ b/tests/stdlib/tenumutils.nim @@ -0,0 +1,16 @@ +discard """ + targets: "c js" +""" + +import std/enumutils +from std/sequtils import toSeq + +template main = + block: # items + type A = enum a0 = 2, a1 = 4, a2 + type B[T] = enum b0 = 2, b1 = 4 + assert A.toSeq == [a0, a1, a2] + assert B[float].toSeq == [B[float].b0, B[float].b1] + +static: main() +main() From cf11fd4f0da5ef2cd0928f93b6a07f209c7d80fd Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 17 Feb 2021 23:49:18 -0800 Subject: [PATCH 09/14] fixup --- lib/std/enumutils.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/enumutils.nim b/lib/std/enumutils.nim index 0bdf736e42e13..4ddd1a41e3d37 100644 --- a/lib/std/enumutils.nim +++ b/lib/std/enumutils.nim @@ -73,7 +73,7 @@ iterator items*[T: enum and not Ordinal](E: typedesc[T]): T = runnableExamples: type A = enum a0 = 2, a1 = 4, a2 type B[T] = enum b0 = 2, b1 = 4 - from sequtils import toSeq + from std/sequtils import toSeq assert A.toSeq == [a0, a1, a2] assert B[float].toSeq == [B[float].b0, B[float].b1] for a in sparseEnumFullRange(E): yield a From 2018d0e11470f454d05a58b34fd63a1911d2b454 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 17 Feb 2021 23:51:25 -0800 Subject: [PATCH 10/14] fixup --- tests/stdlib/tenumutils.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/stdlib/tenumutils.nim b/tests/stdlib/tenumutils.nim index 6dbcb4308144b..dd5da19748562 100644 --- a/tests/stdlib/tenumutils.nim +++ b/tests/stdlib/tenumutils.nim @@ -9,8 +9,8 @@ template main = block: # items type A = enum a0 = 2, a1 = 4, a2 type B[T] = enum b0 = 2, b1 = 4 - assert A.toSeq == [a0, a1, a2] - assert B[float].toSeq == [B[float].b0, B[float].b1] + doAssert A.toSeq == [a0, a1, a2] + doAssert B[float].toSeq == [B[float].b0, B[float].b1] static: main() main() From 293f1119c1ddf7f3e80b2c9fb8ce793e8395a9de Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 17 Feb 2021 23:55:18 -0800 Subject: [PATCH 11/14] ref in lib.rst --- doc/lib.rst | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/lib.rst b/doc/lib.rst index 11b479902ceec..c01e6df535383 100644 --- a/doc/lib.rst +++ b/doc/lib.rst @@ -89,9 +89,6 @@ Algorithms This module implements operations for the built-in ``seq`` type which were inspired by functional programming languages. -* `std/setutils `_ - This module adds functionality for the built-in ``set`` type. - Collections ----------- @@ -123,6 +120,12 @@ Collections * `sets `_ Nim hash and bit set support. +* `std/setutils `_ + This module adds functionality for the built-in ``set`` type. + +* `std/enumutils `_ + This module adds functionality for the built-in ``enum`` type. + * `sharedlist `_ Nim shared linked list support. Contains a shared singly-linked list. From 7abe7c0ffbd2cc65c8970dcf36277c6578b6b606 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Sat, 20 Feb 2021 00:50:07 -0800 Subject: [PATCH 12/14] use `type SomeSparseEnum* = (not Ordinal) and enum` instead of concept --- lib/pure/typetraits.nim | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim index 1ef6f78e054d1..38c22feb300d0 100644 --- a/lib/pure/typetraits.nim +++ b/lib/pure/typetraits.nim @@ -15,8 +15,7 @@ import std/private/since export system.`$` # for backward compatibility -type SomeSparseEnum* = concept x ## Type class for sparse enum's. - x is enum and x isnot Ordinal +type SomeSparseEnum* = (not Ordinal) and enum ## sparse enum's, a.k.a enum with holes #[ xxx `runnableExamples` isn't run if inside: From 185123766f19122dd30106e19a4ebd24687fec33 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Tue, 23 Feb 2021 00:49:55 -0800 Subject: [PATCH 13/14] address comments --- doc/lib.rst | 12 ++++++------ lib/pure/typetraits.nim | 2 +- lib/std/enumutils.nim | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/doc/lib.rst b/doc/lib.rst index c01e6df535383..47ede70ee8c7e 100644 --- a/doc/lib.rst +++ b/doc/lib.rst @@ -85,10 +85,16 @@ Algorithms * `algorithm `_ This module implements some common generic algorithms like sort or binary search. +* `std/enumutils `_ + This module adds functionality for the built-in ``enum`` type. + * `sequtils `_ This module implements operations for the built-in ``seq`` type which were inspired by functional programming languages. +* `std/setutils `_ + This module adds functionality for the built-in ``set`` type. + Collections ----------- @@ -120,12 +126,6 @@ Collections * `sets `_ Nim hash and bit set support. -* `std/setutils `_ - This module adds functionality for the built-in ``set`` type. - -* `std/enumutils `_ - This module adds functionality for the built-in ``enum`` type. - * `sharedlist `_ Nim shared linked list support. Contains a shared singly-linked list. diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim index 38c22feb300d0..888168cb7408a 100644 --- a/lib/pure/typetraits.nim +++ b/lib/pure/typetraits.nim @@ -15,7 +15,7 @@ import std/private/since export system.`$` # for backward compatibility -type SomeSparseEnum* = (not Ordinal) and enum ## sparse enum's, a.k.a enum with holes +type SomeSparseEnum* = (not Ordinal) and enum ## Sparse enum's, a.k.a enum with holes. #[ xxx `runnableExamples` isn't run if inside: diff --git a/lib/std/enumutils.nim b/lib/std/enumutils.nim index 4ddd1a41e3d37..fd8fe99bb3ed6 100644 --- a/lib/std/enumutils.nim +++ b/lib/std/enumutils.nim @@ -69,7 +69,7 @@ macro sparseEnumFullRange(a: typed): untyped = newNimNode(nnkCurly).add(a.getType[1][1..^1]) iterator items*[T: enum and not Ordinal](E: typedesc[T]): T = - ## iterates over a sparse enum + ## Iterates over a sparse enum. runnableExamples: type A = enum a0 = 2, a1 = 4, a2 type B[T] = enum b0 = 2, b1 = 4 From d1a3a52b46e0e4a9aaa95063adb8fb8ccd38c431 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Tue, 23 Feb 2021 01:16:54 -0800 Subject: [PATCH 14/14] address comment: rename back to enum with holes --- changelog.md | 4 ++-- lib/pure/typetraits.nim | 17 +++++------------ lib/std/enumutils.nim | 6 +++--- lib/system/iterators.nim | 2 +- 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/changelog.md b/changelog.md index 41e10d37347f6..17844e09a165a 100644 --- a/changelog.md +++ b/changelog.md @@ -42,9 +42,9 @@ Useful for library authors. - Added `std/enumutils` module. Added `genEnumCaseStmt` macro that generates case statement to parse string to enum. - Added `items` for sparse enums. + Added `items` for enums with holes. -- Added `typetraits.SomeSparseEnum` for sparse enums. +- Added `typetraits.SomeEnumWithHoles` for enums with holes. - Removed deprecated `iup` module from stdlib, it has already moved to [nimble](https://github.com/nim-lang/iup). diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim index 888168cb7408a..69c0329ef1a5b 100644 --- a/lib/pure/typetraits.nim +++ b/lib/pure/typetraits.nim @@ -15,23 +15,16 @@ import std/private/since export system.`$` # for backward compatibility -type SomeSparseEnum* = (not Ordinal) and enum ## Sparse enum's, a.k.a enum with holes. - -#[ -xxx `runnableExamples` isn't run if inside: -type Foo* = concept x - runnableExamples: assert false - x is int -]# +type SomeEnumWithHoles* = (not Ordinal) and enum ## Enum with holes. runnableExamples: type A = enum a0 = 2, a1 = 4, a2 type B = enum b0 = 2, b1, b2 - assert A is SomeSparseEnum - assert B isnot SomeSparseEnum - assert int isnot SomeSparseEnum + assert A is SomeEnumWithHoles + assert B isnot SomeEnumWithHoles + assert int isnot SomeEnumWithHoles type C[T] = enum h0 = 2, h1 = 4 - assert C[float] is SomeSparseEnum + assert C[float] is SomeEnumWithHoles proc name*(t: typedesc): string {.magic: "TypeTrait".} = ## Returns the name of the given type. diff --git a/lib/std/enumutils.nim b/lib/std/enumutils.nim index fd8fe99bb3ed6..56a6d82a7e7d5 100644 --- a/lib/std/enumutils.nim +++ b/lib/std/enumutils.nim @@ -65,15 +65,15 @@ macro genEnumCaseStmt*(typ: typedesc, argSym: typed, default: typed, expectKind(default, nnkSym) result.add nnkElse.newTree(default) -macro sparseEnumFullRange(a: typed): untyped = +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 a sparse enum. + ## 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 sparseEnumFullRange(E): yield a + for a in enumWithHolesFullRange(E): yield a diff --git a/lib/system/iterators.nim b/lib/system/iterators.nim index 58a1fdb5cd7e2..7f0076a444652 100644 --- a/lib/system/iterators.nim +++ b/lib/system/iterators.nim @@ -84,7 +84,7 @@ iterator mitems*(a: var cstring): var char {.inline.} = iterator items*[T: enum and Ordinal](E: typedesc[T]): T = ## Iterates over the values of `E`. - ## See also `enumutils.items` for sparse enums. + ## See also `enumutils.items` for enums with holes. runnableExamples: type Goo = enum g0 = 2, g1, g2 from std/sequtils import toSeq