cdunn2001: "There's actually a lot of places I'd really like to use "withAs"
in, eg to make sure that resources are closed, but without needing to add a
resource-specific "defer" line." So wizzardx will need to create a template
like this for every type he'll use
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:
@Benjaminel: I'm trying to emulate the Python "with" style, which is more
general-purpose than just files. I think that "using" in C# is similar, to take
any given resource for a block, and then auto-close it for you at the end of
that block.
@Others:
Thanks for the replies so far! So, I
Why not do something like:
template withFile(f: string, mode: string, statements: untyped) =
var fileMode: FileMode
case mode
of "r": fileMode = FileMode.fmRead
of "w": fileMode = FileMode.fmWrite
of "a+":fileMode =
It could be helpful to support something like the `contextmanager` decorator in
Python, which lets the user specify "enter" and "exit" as the code before and
after `yield`, e.g.
*
[https://stackoverflow.com/a/24176022/263998](https://stackoverflow.com/a/24176022/263998)
*
I don't think is worth the effort for roughly 5 or 6 template lines, but i
think there is already a pythonesque library in nimble.
Thanks for the replies!
well, no, only "with" and "without":
[https://github.com/nim-lang/Nim/commit/3ccc9c467d84dc8c3412acbea20fc10b5335eaa8](https://github.com/nim-lang/Nim/commit/3ccc9c467d84dc8c3412acbea20fc10b5335eaa8)
@Tiberium Oh, that's wonderful! I like Was interface removed too (I'd really
like to use it sometimes)?
Udiknedormin: you can change "take" to "with", since "with" and "without" were
removed from Nim keyword list in devel
As for now, template + getAst seems preferred over quote. Please note it
provides you template hygiene, meaning the symbols are generated, thus your
private variable will actually be private (as it will be treated as a new
_symbol_ rather than an identifier).
import macros
It's very easy with "quote do":
import macros
proc my_enter(x: int): int =
echo "my_enter was called"
return x
proc my_exit(x: int) =
echo "my_exit was called"
macro take(args, body: untyped): untyped =
# Value of an argument
Thanks!
So if I understand it correctly, this macro usage in your snippet:
take 3 as f:
echo f
Gets expanded to this:
var f = 3
echo f
How can we change the same macro, so that it desugars over to this instead?
var x = 3
var f
`with` is a keyword, so you can't use that. You could use something like this
maybe?
import macros
macro take(args,body:untyped):untyped =
result = newstmtlist()
result.add(newvarstmt(args[^1],args[1]))
for st in body: result.add(st)
take 3 as f:
Thanks for the replies
Is it possible to express the "withAs" template as a macro?
So that my example could look like this:
with open("test.txt") as f:
echo f.readLine()
I think it should be possible, since eg nimpylib lets you use this syntax:
class
@wizzardx and @Ar , it's wonderful to have this feature. Thanks for the
code snippets.
You can use defer for closing resources:
[https://nim-lang.org/docs/manual.html#exception-handling-defer-statement](https://nim-lang.org/docs/manual.html#exception-handling-defer-statement)
17 matches
Mail list logo