Re: Python-like with, context managers, and the RAII pattern

2017-10-07 Thread Tiberium
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

Re: Python-like with, context managers, and the RAII pattern

2017-10-07 Thread cdunn2001
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:

Re: Python-like with, context managers, and the RAII pattern

2017-10-05 Thread wizzardx
@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

Re: Python-like with, context managers, and the RAII pattern

2017-10-01 Thread Benjaminel
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 =

Re: Python-like with, context managers, and the RAII pattern

2017-10-01 Thread cdunn2001
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) *

Re: Python-like with, context managers, and the RAII pattern

2017-10-01 Thread Arrrrrrrrr
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.

Re: Python-like with, context managers, and the RAII pattern

2017-10-01 Thread wizzardx
Thanks for the replies!

Re: Python-like with, context managers, and the RAII pattern

2017-09-30 Thread Tiberium
well, no, only "with" and "without": [https://github.com/nim-lang/Nim/commit/3ccc9c467d84dc8c3412acbea20fc10b5335eaa8](https://github.com/nim-lang/Nim/commit/3ccc9c467d84dc8c3412acbea20fc10b5335eaa8)

Re: Python-like with, context managers, and the RAII pattern

2017-09-30 Thread Udiknedormin
@Tiberium Oh, that's wonderful! I like Was interface removed too (I'd really like to use it sometimes)?

Re: Python-like with, context managers, and the RAII pattern

2017-09-30 Thread Tiberium
Udiknedormin: you can change "take" to "with", since "with" and "without" were removed from Nim keyword list in devel

Re: Python-like with, context managers, and the RAII pattern

2017-09-30 Thread Udiknedormin
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

Re: Python-like with, context managers, and the RAII pattern

2017-09-30 Thread Tiberium
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

Re: Python-like with, context managers, and the RAII pattern

2017-09-30 Thread wizzardx
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

Re: Python-like with, context managers, and the RAII pattern

2017-09-29 Thread stisa
`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:

Re: Python-like with, context managers, and the RAII pattern

2017-09-29 Thread wizzardx
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

Re: Python-like with, context managers, and the RAII pattern

2017-09-28 Thread pgmf
@wizzardx and @Ar , it's wonderful to have this feature. Thanks for the code snippets.

Re: Python-like with, context managers, and the RAII pattern

2017-09-28 Thread Tiberium
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)