digitalmars.D.learn - Equivalent to Python with Statement
- Jonathan (9/9) Feb 27 2018 I know Python's `with` statement can be used to have an automatic
- Stefan Koch (3/12) Feb 27 2018 In this case with(File("bla"))
- Jonathan (4/17) Feb 27 2018 Oh really, cool.
- Daniel Kozak (25/46) Feb 27 2018 yes, for classes you can use scoped:
- Seb (8/21) Feb 27 2018 FWIW std.stdio.File is refcounted and will be automatically
- Cym13 (12/21) Feb 28 2018 Others have discussed that particular case at length, but to
- meppl (3/16) Feb 28 2018 what is `scope(out)`? I only know these
- meppl (3/23) Feb 28 2018 sorry, my question was not good. I thought there is an
- Seb (5/27) Feb 28 2018 I know that I am repeating myself, but manually closing the file
- Cym13 (5/36) Feb 28 2018 As stated, yes that's an accurate answer to that particular
- phongkhamdakhoathegioi (2/14) Mar 03 2018 http://phongkhamdakhoathegioi.vn/u-nang-buong-trung-dang-nuoc.html
I know Python's `with` statement can be used to have an automatic
close action:
```
with open("x.txt") as file:
#do something with file
```
I know D's `with` statement does something different but is there
some sort of equivalent?
Feb 27 2018
On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:
I know Python's `with` statement can be used to have an
automatic close action:
```
with open("x.txt") as file:
#do something with file
```
I know D's `with` statement does something different but is
there some sort of equivalent?
In this case with(File("bla"))
will do the same.
Feb 27 2018
On Tuesday, 27 February 2018 at 16:18:43 UTC, Stefan Koch wrote:On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:Oh really, cool. Is this just because the scope of the file variable will end and thus its `~this`?I know Python's `with` statement can be used to have an automatic close action: ``` with open("x.txt") as file: #do something with file ``` I know D's `with` statement does something different but is there some sort of equivalent?In this case with(File("bla")) will do the same.
Feb 27 2018
yes, for classes you can use scoped:
import std.stdio;
import std.typecons : scoped;
class A
{
void saySomething()
{
writeln("Hi from A");
}
~this()
{
writeln("Destruct A");
}
}
void main()
{
with(scoped!A())
{
saySomething();
writeln("something");
}
writeln("Hello D");
}
On Tue, Feb 27, 2018 at 5:25 PM, Jonathan via Digitalmars-d-learn <
digitalmars-d-learn puremagic.com> wrote:
On Tuesday, 27 February 2018 at 16:18:43 UTC, Stefan Koch wrote:
On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:
I know Python's `with` statement can be used to have an automatic close
action:
```
with open("x.txt") as file:
#do something with file
```
I know D's `with` statement does something different but is there some
sort of equivalent?
In this case with(File("bla"))
will do the same.
Oh really, cool.
Is this just because the scope of the file variable will end and thus its
`~this`?
Feb 27 2018
On Tuesday, 27 February 2018 at 16:18:43 UTC, Stefan Koch wrote:On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:FWIW std.stdio.File is refcounted and will be automatically closed at the end of the scope. So typically you don't even need `with` ;-) Jonathan: have a look at these two examples to see what DMD does under the hood: https://run.dlang.io/is/89NBxI https://run.dlang.io/is/eXC7ZVI know Python's `with` statement can be used to have an automatic close action: ``` with open("x.txt") as file: #do something with file ``` I know D's `with` statement does something different but is there some sort of equivalent?In this case with(File("bla")) will do the same.
Feb 27 2018
On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:
I know Python's `with` statement can be used to have an
automatic close action:
```
with open("x.txt") as file:
#do something with file
```
I know D's `with` statement does something different but is
there some sort of equivalent?
Others have discussed that particular case at length, but to
provide a more generic answer the correct way to translate a
python context manager is to use scope(out):
{ // Scope restriction similar to with's block
auto f = File("x.txt");
scope(out) f.close(); // closes f on scope exit no
matter what
/* do stuff with f */
}
This accurately reproduces the behaviour of python's with
although it's less automatic.
Feb 28 2018
On Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 wrote:On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:what is `scope(out)`? I only know these file:///usr/share/dmd-doc/html/d/spec/statement.html#ScopeGuardStatement[...]Others have discussed that particular case at length, but to provide a more generic answer the correct way to translate a python context manager is to use scope(out): { // Scope restriction similar to with's block auto f = File("x.txt"); scope(out) f.close(); // closes f on scope exit no matter what /* do stuff with f */ } This accurately reproduces the behaviour of python's with although it's less automatic.
Feb 28 2018
On Wednesday, 28 February 2018 at 22:00:11 UTC, meppl wrote:On Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 wrote:sorry, my question was not good. I thought there is an undocumented feature, never mind ;)On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:what is `scope(out)`? I only know these file:///usr/share/dmd-doc/html/d/spec/statement.html#ScopeGuardStatement[...]Others have discussed that particular case at length, but to provide a more generic answer the correct way to translate a python context manager is to use scope(out): { // Scope restriction similar to with's block auto f = File("x.txt"); scope(out) f.close(); // closes f on scope exit no matter what /* do stuff with f */ } This accurately reproduces the behaviour of python's with although it's less automatic.
Feb 28 2018
On Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 wrote:On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:I know that I am repeating myself, but manually closing the file isn't needed in D. It's refCounted and will be closed automatically once the scope is left. That's why `with(File("AAA","w"))` works in D.I know Python's `with` statement can be used to have an automatic close action: ``` with open("x.txt") as file: #do something with file ``` I know D's `with` statement does something different but is there some sort of equivalent?Others have discussed that particular case at length, but to provide a more generic answer the correct way to translate a python context manager is to use scope(out): { // Scope restriction similar to with's block auto f = File("x.txt"); scope(out) f.close(); // closes f on scope exit no matter what /* do stuff with f */ } This accurately reproduces the behaviour of python's with although it's less automatic.
Feb 28 2018
On Wednesday, 28 February 2018 at 22:55:19 UTC, Seb wrote:On Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 wrote:As stated, yes that's an accurate answer to that particular problem and it's well understood, I'm just giving a more general answer that will work for every python context manager and not only on the specific case of closing files.On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:I know that I am repeating myself, but manually closing the file isn't needed in D. It's refCounted and will be closed automatically once the scope is left. That's why `with(File("AAA","w"))` works in D.I know Python's `with` statement can be used to have an automatic close action: ``` with open("x.txt") as file: #do something with file ``` I know D's `with` statement does something different but is there some sort of equivalent?Others have discussed that particular case at length, but to provide a more generic answer the correct way to translate a python context manager is to use scope(out): { // Scope restriction similar to with's block auto f = File("x.txt"); scope(out) f.close(); // closes f on scope exit no matter what /* do stuff with f */ } This accurately reproduces the behaviour of python's with although it's less automatic.
Feb 28 2018
On Thursday, 1 March 2018 at 07:34:38 UTC, Cym13 wrote:On Wednesday, 28 February 2018 at 22:55:19 UTC, Seb wrote:http://phongkhamdakhoathegioi.vn/u-nang-buong-trung-dang-nuoc.htmlOn Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 wrote:As stated, yes that's an accurate answer to that particular problem and it's well understood, I'm just giving a more general answer that will work for every python context manager and not only on the specific case of closing files.[...]I know that I am repeating myself, but manually closing the file isn't needed in D. It's refCounted and will be closed automatically once the scope is left. That's why `with(File("AAA","w"))` works in D.
Mar 03 2018









Daniel Kozak <kozzi11 gmail.com> 