newLit works on enum (#9662)

* newLit works on enum

* remove debugging echo
This commit is contained in:
Arne Döring 2018-11-09 12:15:00 +01:00 • committed by GitHub
commit 573d02760e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View file

@ -696,6 +696,16 @@ proc newLit*[T](arg: seq[T]): NimNode {.compileTime.} =
), ),
bracket bracket
) )
proc newLit*(arg: enum): NimNode {.compileTime.} =
result = newCall(
arg.type.getTypeInst[1],
newLit(int(arg))
)
proc newLit*[T](s: set[T]): NimNode {.compileTime.} =
result = nnkCurly.newTree
for x in s:
result.add newLit(x)
proc newLit*(arg: tuple): NimNode {.compileTime.} = proc newLit*(arg: tuple): NimNode {.compileTime.} =
result = nnkPar.newTree result = nnkPar.newTree

View file

@ -147,3 +147,23 @@ block:
# x needs to be of type seq[string] # x needs to be of type seq[string]
var x = test_newLit_empty_seq_string var x = test_newLit_empty_seq_string
x.add("xyz") x.add("xyz")
type
MyEnum = enum
meA
meB
macro test_newLit_Enum: untyped =
result = newLit(meA)
block:
let tmp: MyEnum = meA
doAssert tmp == test_newLit_Enum
macro test_newLit_set: untyped =
let myset = {MyEnum.low .. MyEnum.high}
result = newLit(myset)
block:
let tmp: set[MyEnum] = {MyEnum.low .. MyEnum.high}
doAssert tmp == test_newLit_set