scoped chdir and similar patterns

anonymous anonymous at example.com
Thu Dec 5 11:12:29 PST 2013


On Thursday, 5 December 2013 at 06:24:52 UTC, qznc wrote:
> =============
> struct chdir_scoped {
>   string olddir;
>   this(string newdir) {
>     olddir = "bar";
>     writeln("chdir to "~newdir);
>   }
>   ~this() {
>     writeln("chdir back to "~olddir);
>   }
> }
>
> int main() {
>   auto x = chdir_scoped("foo");
>   writeln("doing work in foo");
>   return 0;
> }
> =============
> Output:
> chdir to foo
> doing work in foo
> chdir back to bar
> =============
>
> Feels hacky to me, since "x" is not used anywhere.

with(chdir_scoped("foo")) writeln("doing work in foo");

Alas: https://d.puremagic.com/issues/show_bug.cgi?id=8269


More information about the Digitalmars-d-learn mailing list