Re: Equivalent to Python with Statement

2018-03-03 Thread phongkhamdakhoathegioi via Digitalmars-d-learn
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

Re: Equivalent to Python with Statement

2018-02-28 Thread Cym13 via Digitalmars-d-learn
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

Re: Equivalent to Python with Statement

2018-02-28 Thread Seb via Digitalmars-d-learn
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 #`file.close()`

Re: Equivalent to Python with Statement

2018-02-28 Thread meppl via Digitalmars-d-learn
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

Re: Equivalent to Python with Statement

2018-02-28 Thread meppl via Digitalmars-d-learn
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

Re: Equivalent to Python with Statement

2018-02-28 Thread Cym13 via Digitalmars-d-learn
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 #`file.close()` called automatically ``` I know D's `with` statement does

Re: Equivalent to Python with Statement

2018-02-27 Thread Seb via Digitalmars-d-learn
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 #`file.close()`

Re: Equivalent to Python with Statement

2018-02-27 Thread Daniel Kozak via Digitalmars-d-learn
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();

Re: Equivalent to Python with Statement

2018-02-27 Thread Jonathan via Digitalmars-d-learn
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 #`file.close()`

Equivalent to Python with Statement

2018-02-27 Thread Jonathan via Digitalmars-d-learn
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 #`file.close()` called automatically ``` I know D's `with` statement does something different but is there some sort of equivalent?

Re: Equivalent to Python with Statement

2018-02-27 Thread Stefan Koch via Digitalmars-d-learn
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 #`file.close()` called automatically ``` I know D's `with` statement does