asarray targetType parameter does not need to be a typedesc.

This commit is contained in:
Fredrik Høisæther Rasch 2017-11-06 14:19:41 +01:00 • committed by Andreas Rumpf
commit e334a257fa

View file

@ -199,7 +199,7 @@ macro dump*(x: typed): untyped =
debugEcho `s`, " = ", `x` debugEcho `s`, " = ", `x`
return r return r
macro asArray*(targetType: typedesc, values: typed): untyped = macro asArray*(targetType: untyped, values: typed): untyped =
## applies a type conversion to each of the elements in the specified ## applies a type conversion to each of the elements in the specified
## array literal. Each element is converted to the ``targetType`` type.. ## array literal. Each element is converted to the ``targetType`` type..
## ##
@ -213,16 +213,18 @@ macro asArray*(targetType: typedesc, values: typed): untyped =
## ##
## .. code-block:: ## .. code-block::
## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int] ## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int]
let tNode = getType(targetType)[1]
values.expectKind(nnkBracket) values.expectKind(nnkBracket)
result = newNimNode(nnkBracket, lineInfoFrom=values) result = newNimNode(nnkBracket, lineInfoFrom=values)
for i in 0 ..< len(values): for i in 0 ..< len(values):
var call = newNimNode(nnkCall, lineInfoFrom=values[i]) var call = newNimNode(nnkCall, lineInfoFrom=values[i])
call.add tNode call.add targetType
call.add values[i] call.add values[i]
result.add call result.add call
echo result.repr()
when isMainModule: when isMainModule:
block: # asArray tests block: # asArray tests
let x = asArray(int, [1.2, 2.3, 3.4, 4.5]) let x = asArray(int, [1.2, 2.3, 3.4, 4.5])
doAssert x is array[4, int] doAssert x is array[4, int]
let y = asArray(`$`, [1.2, 2.3, 3.4, 4.5])
doAssert y is array[4, string]