for example I have finished a code which uses echo to show every messages, for example:
var a = 10
var b = 7
# the following are my professsional code
a += 1
echo $a
echo " * "
echo $b
echo " = "
echo $(a*b)
Now I want to wrap the code as a function, but I am too lazy to rewrite previous echo to string operation, so is there a lazy way, for example
proc fn(a:int, b:int): string =
var capturedEcho = something
# the following are my professsional code
a += 1
echo $a
echo " * "
echo $b
echo " = "
echo $(a*b)
# now, all the `echo` result, including CRLF, has been recorded
# so we can just close redirection and return the captured string
capturedEcho.close
return capturedEcho
thanks
Just rewrite the code.
Now with that suggestion given, one can shadow the procedure if they really wanted to as such:
proc fn(a:int, b:int): string =
let res = result.addr
var echo = proc(args: varargs[string, `$`]) =
for arg in args:
res[].add arg
# the following are my professsional code
let a = a + 1
echo a
echo " * "
echo b
echo " = "
echo (a*b)
result.add "\n"
echo fn(20, 30)
I second the notion of just search replacing echo with a sensible alternative. result.add might not be enough, as it won't auto stringify for example. But with a little helper proc it's fairly easy.
That being said, and more for completeness than anything, I did write this a while back: https://github.com/PMunch/echooverride