www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Equivalent to Python with Statement

reply Jonathan <JonathanILevi gmail.com> writes:
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
next sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
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
next sibling parent reply Jonathan <JonathanILevi gmail.com> writes:
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
parent Daniel Kozak <kozzi11 gmail.com> writes:
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
prev sibling parent Seb <seb wilzba.ch> writes:
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.
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/eXC7ZV
Feb 27 2018
prev sibling parent reply Cym13 <cpicard openmailbox.org> writes:
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
next sibling parent reply meppl <mephisto nordhoff-online.de> writes:
On Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 wrote:
 On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:
 [...]
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.
what is `scope(out)`? I only know these file:///usr/share/dmd-doc/html/d/spec/statement.html#ScopeGuardStatement
Feb 28 2018
parent meppl <mephisto nordhoff-online.de> writes:
On Wednesday, 28 February 2018 at 22:00:11 UTC, meppl wrote:
 On Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 wrote:
 On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:
 [...]
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.
what is `scope(out)`? I only know these file:///usr/share/dmd-doc/html/d/spec/statement.html#ScopeGuardStatement
sorry, my question was not good. I thought there is an undocumented feature, never mind ;)
Feb 28 2018
prev sibling parent reply Seb <seb wilzba.ch> writes:
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 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.
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.
Feb 28 2018
parent reply Cym13 <cpicard openmailbox.org> writes:
On Wednesday, 28 February 2018 at 22:55:19 UTC, Seb wrote:
 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 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.
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.
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.
Feb 28 2018
parent phongkhamdakhoathegioi <thanhbinh04101988 gmail.com> writes:
On Thursday, 1 March 2018 at 07:34:38 UTC, Cym13 wrote:
 On Wednesday, 28 February 2018 at 22:55:19 UTC, Seb wrote:
 On Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 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.
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.
http://phongkhamdakhoathegioi.vn/u-nang-buong-trung-dang-nuoc.html
Mar 03 2018