My current extension of @Araq's macro looks like this:
import macros, typetraits
proc replace(n: NimNode; typesrc,typedst: typed; kndsrc,knddst:
NimNodeKind): NimNode =
if n.kind == kndsrc:
when not defined(release): echo "replacing ",n.repr," with ",knddst
result = newNimNode(knddst)
case kndsrc:
of nnkFloatLit: result.floatVal = n.floatVal
of nnkIntLit: result.intVal = n.intVal
else: discard
elif n.repr == typesrc.repr:
when not defined(release): echo "replacing ",n.repr," with
",typedst.repr
result = newIdentNode(typedst.repr)
else:
result = copyNimNode(n)
for i in 0..<n.len: result.add replace(n[i], typesrc, typedst, kndsrc,
knddst)
macro defaultf32(n: untyped): untyped = replace(n, float, float32,
nnkFloatLit, nnkFloat32Lit)
macro defaulti32(n: untyped): untyped = replace(n, int, int32, nnkIntLit,
nnkInt32Lit)
Run
, which implies you can do
proc doit() {.defaulti32.} =
var a:array[3,int] = [1,3,6]
for i in 0 ..< 3:
echo a[i]
doAssert i is int32
doAssert a[i] is int32
for i in low(a) .. high(a):
echo a[i]
doAssert i is int32
doAssert a[i] is int32
Run