<div dir="ltr">yes, for classes you can use scoped:<div><br></div><div><div>import std.stdio;</div><div>import std.typecons : scoped;</div><div>class A</div><div>{</div><div>    void saySomething()</div><div>    {</div><div>    <span style="white-space:pre">  </span>writeln("Hi from A");    </div><div>    }</div><div>    ~this()</div><div>    {</div><div>        writeln("Destruct A");</div><div>    }</div><div>}</div><div><br></div><div>void main()</div><div>{</div><div>    with(scoped!A())</div><div>    {</div><div>        saySomething();</div><div>        writeln("something");</div><div>    }</div><div>    writeln("Hello D");</div><div>}</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Feb 27, 2018 at 5:25 PM, Jonathan via Digitalmars-d-learn <span dir="ltr"><<a href="mailto:digitalmars-d-learn@puremagic.com" target="_blank">digitalmars-d-learn@puremagic.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Tuesday, 27 February 2018 at 16:18:43 UTC, Stefan Koch wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I know Python's `with` statement can be used to have an automatic close action:<br>
```<br>
    with open("x.txt") as file:<br>
        #do something with file<br>
    #`file.close()` called automatically<br>
```<br>
<br>
I know D's `with` statement does something different but is there some sort of equivalent?<br>
</blockquote>
<br>
In this case with(File("bla"))<br>
will do the same.<br>
</blockquote>
<br></div></div>
Oh really, cool.<br>
<br>
Is this just because the scope of the file variable will end and thus its `~this`?<br>
<br>
</blockquote></div><br></div>