digitalmars.D.learn - Does D have any construct like Python's with keyword?
- pineapple (3/3) Aug 26 2016 I've grown to very much appreciate how context initialization and
- Cauterite (4/7) Aug 26 2016 Yep, scope guards.
- pineapple (27/34) Aug 26 2016 What I had in mind was something more like this, but not quite so
- pineapple (4/4) Aug 26 2016 I would just love if I could express this as something more like
- Adam D. Ruppe (13/16) Aug 26 2016 You don't need to do anything special for that in D, structs are
- Daniel Kozak via Digitalmars-d-learn (8/12) Aug 27 2016 void main(string[] args)
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
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
On Friday, 26 August 2016 at 23:30:15 UTC, Cauterite wrote:On Friday, 26 August 2016 at 23:28:27 UTC, pineapple wrote: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(); }); }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
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
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
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