Skip to content

Commit

Permalink
rename to SomeSparseEnum etc
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Feb 18, 2021
1 parent 16f95f0 commit ebc2343
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,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).
Expand Down
14 changes: 7 additions & 7 deletions lib/pure/typetraits.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions lib/std/enumutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/system/iterators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ebc2343

Please sign in to comment.