Thread overview
Does D have any construct like Python's with keyword?
Aug 26, 2016
pineapple
Aug 26, 2016
Cauterite
Aug 27, 2016
pineapple
Aug 27, 2016
pineapple
Aug 27, 2016
Adam D. Ruppe
Aug 27, 2016
Daniel Kozak
August 26, 2016
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?
August 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);};
August 27, 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:
>> 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();
        });
    }

August 27, 2016
I would just love if I could express this as something more like


    context(auto file = File("some_file.txt")){
        file.write();
    }

August 27, 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 }.
August 27, 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();
    }
}