I want to use std/complex library as complex[Float] where Float is my original object other than float, float32, float64. I have already defined all function of Float like sin, cos, tan etc to use Complex. But the definition of object Complex is written as "type Complex*[T: SomeFloat] = object ... " and I cannot use complex.
https://github.com/nim-lang/Nim/blob/version-2-0/lib/pure/complex.nim#L1
Simple solution for this is to copy std/complex and erase SomeFloat. But I want to use it as standard library. Is there any way?
In this case those environmental miss issues are due to having nkSym inside the ast still, this can be resolved with the awful recursive iteration and replace of the AST.
import macros
proc cleanUp(n: NimNode) =
for i, x in n:
case x.kind
of nnkSym:
n[i] = ident $x
of nnkHiddenStdConv:
cleanup x[^1]
n[i] = x[^1][0]
cleanup(n[i])
of nnkHiddenCallConv:
cleanup x
n[i] = x[^1]
cleanup(n[i])
else:
cleanup(x)
macro redef(x: typed, arg: untyped, newType: typed): untyped =
result = x.getImpl
result[0] = ident $x
assert result.kind == nnkProcDef
for n in result[3]:
if n.kind == nnkIdentDefs:
for i in 0 .. n.len - 2:
n[i] = ident $n[i]
if n[0].eqIdent arg:
n[^2] = newType
result[^1].cleanUp()
x.getImpl.treerepr.echo
result.treeRepr.echo
proc someProc(x: int) = echo x
redef(someProc, x, float)
someProc(42)
someProc(100.10)
It reminds me of "monkey patching" in Python
Man I really hated it back then