Equivalent to Python with Statement

meppl mephisto at nordhoff-online.de
Wed Feb 28 22:00:11 UTC 2018


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 scope(out):
>
>     { // Scope restriction similar to with's block
>         auto f = File("x.txt");
>         scope(out) f.close();   // closes f on scope exit no 
> matter what
>
>         /* do stuff with f */
>     }
>
> This accurately reproduces the behaviour of python's with 
> although it's less automatic.

what is `scope(out)`? I only know these  
file:///usr/share/dmd-doc/html/d/spec/statement.html#ScopeGuardStatement


More information about the Digitalmars-d-learn mailing list