Since you don't want to dive into macros and you don't need anything
super-generic, I think you're better off with @Benjaminel's idea:
template withcd(newdir: string, statements: untyped) =
let olddir = os.getCurrentDir()
os.setCurrentDir(newdir)
defer: os.setCurrentDir(olddir)
statements
import os
proc hi() =
echo "in:", os.getCurrentDir()
hi()
withcd("bar"):
echo "It works!"
hi()
echo "Finish it."
hi()
in:/foo
It works!
in:/foo/bar
Finish it.
in:/foo
- Re: Python-like with, context managers, and the RAII pattern cdunn2001
- Re: Python-like with, context managers, and the RAII patte... Benjaminel
- Re: Python-like with, context managers, and the RAII p... wizzardx
- Re: Python-like with, context managers, and the RA... cdunn2001
