macros.newLit now works for ref object types (#12307)
This commit is contained in:
parent
dd082b6ec8
commit
a4ade43536
3 changed files with 33 additions and 0 deletions
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
## Library additions
|
## Library additions
|
||||||
|
|
||||||
|
- `macros.newLit` now works for ref object types.
|
||||||
|
|
||||||
|
|
||||||
## Library changes
|
## Library changes
|
||||||
|
|
@ -41,3 +42,4 @@
|
||||||
|
|
||||||
|
|
||||||
## Bugfixes
|
## Bugfixes
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -742,6 +742,12 @@ proc newLit*(arg: object): NimNode {.compileTime.} =
|
||||||
for a, b in arg.fieldPairs:
|
for a, b in arg.fieldPairs:
|
||||||
result.add nnkExprColonExpr.newTree( newIdentNode(a), newLit(b) )
|
result.add nnkExprColonExpr.newTree( newIdentNode(a), newLit(b) )
|
||||||
|
|
||||||
|
proc newLit*(arg: ref object): NimNode {.compileTime.} =
|
||||||
|
## produces a new ref type literal node.
|
||||||
|
result = nnkObjConstr.newTree(arg.type.getTypeInst[1])
|
||||||
|
for a, b in fieldPairs(arg[]):
|
||||||
|
result.add nnkExprColonExpr.newTree(newIdentNode(a), newLit(b))
|
||||||
|
|
||||||
proc newLit*[N,T](arg: array[N,T]): NimNode {.compileTime.} =
|
proc newLit*[N,T](arg: array[N,T]): NimNode {.compileTime.} =
|
||||||
result = nnkBracket.newTree
|
result = nnkBracket.newTree
|
||||||
for x in arg:
|
for x in arg:
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,14 @@ type
|
||||||
a : int
|
a : int
|
||||||
b : string
|
b : string
|
||||||
|
|
||||||
|
RefObject = ref object
|
||||||
|
x: int
|
||||||
|
|
||||||
|
RegularObject = object
|
||||||
|
x: int
|
||||||
|
|
||||||
|
ObjectRefAlias = ref RegularObject
|
||||||
|
|
||||||
macro test_newLit_MyType: untyped =
|
macro test_newLit_MyType: untyped =
|
||||||
let mt = MyType(a: 123, b:"foobar")
|
let mt = MyType(a: 123, b:"foobar")
|
||||||
result = newLit(mt)
|
result = newLit(mt)
|
||||||
|
|
@ -167,3 +175,20 @@ macro test_newLit_set: untyped =
|
||||||
block:
|
block:
|
||||||
let tmp: set[MyEnum] = {MyEnum.low .. MyEnum.high}
|
let tmp: set[MyEnum] = {MyEnum.low .. MyEnum.high}
|
||||||
doAssert tmp == test_newLit_set
|
doAssert tmp == test_newLit_set
|
||||||
|
|
||||||
|
macro test_newLit_ref_object: untyped =
|
||||||
|
var x = RefObject(x: 10)
|
||||||
|
return newLit(x)
|
||||||
|
|
||||||
|
block:
|
||||||
|
let x = test_newLit_ref_object()
|
||||||
|
doAssert $(x[]) == "(x: 10)"
|
||||||
|
|
||||||
|
macro test_newLit_object_ref_alias: untyped =
|
||||||
|
var x = ObjectRefAlias(x: 10)
|
||||||
|
return newLit(x)
|
||||||
|
|
||||||
|
block:
|
||||||
|
let x = test_newLit_object_ref_alias()
|
||||||
|
doAssert $(x[]) == "(x: 10)"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue