www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does D have any construct like Python's with keyword?

reply pineapple <meapineapple gmail.com> writes:
I've grown to very much appreciate how context initialization and 
teardown can be very conveniently handled using `with` in Python. 
Is there any clean way to imitate this syntax in D?
Aug 26 2016
parent reply Cauterite <cauterite gmail.com> writes:
On Friday, 26 August 2016 at 23:28:27 UTC, pineapple wrote:
 I've grown to very much appreciate how context initialization 
 and teardown can be very conveniently handled using `with` in 
 Python. Is there any clean way to imitate this syntax in D?
Yep, scope guards. auto p = OpenProcess(...); scope(exit) {CloseHandle(p);};
Aug 26 2016
parent reply pineapple <meapineapple gmail.com> writes:
On Friday, 26 August 2016 at 23:30:15 UTC, Cauterite wrote:
 On Friday, 26 August 2016 at 23:28:27 UTC, pineapple wrote:
 I've grown to very much appreciate how context initialization 
 and teardown can be very conveniently handled using `with` in 
 Python. Is there any clean way to imitate this syntax in D?
Yep, scope guards. auto p = OpenProcess(...); scope(exit) {CloseHandle(p);};
What I had in mind was something more like this, but not quite so messy-looking. Though after having actually written it out I'm wondering if it's not better to just keep it looking like this, or similar, rather than make it part of the language. It's not _too_ hideous. import std.stdio; void context(Context, Dg)(Context context, Dg content){ scope(exit) context.conclude; content(context); } struct File{ this(string path){ writeln("open a file"); } void write(){ writeln("write some data"); } void conclude(){ writeln("close the file"); } } void main(){ context(File("some_file.txt"), (File file){ file.write(); }); }
Aug 26 2016
parent reply pineapple <meapineapple gmail.com> writes:
I would just love if I could express this as something more like


     context(auto file = File("some_file.txt")){
         file.write();
     }
Aug 26 2016
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 27 August 2016 at 00:04:47 UTC, pineapple wrote:
     context(auto file = File("some_file.txt")){
         file.write();
     }
You don't need to do anything special for that in D, structs are destructed automatically. Plain auto file = File("some_file.txt"); file.write(); will automatically close file when it goes out of scope. If you want it to go out of scope earlier, you can: { auto file = File("some_file.txt"); file.write(); } the curly braces around it will make a new scope, so the destructor, which closes the file, will be called at the }.
Aug 26 2016
prev sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Dne 27.8.2016 v 02:04 pineapple via Digitalmars-d-learn napsal(a):

 I would just love if I could express this as something more like


     context(auto file = File("some_file.txt")){
         file.write();
     }
void main(string[] args) { with(File("some_file.txt")) { write(); } }
Aug 27 2016