scoped chdir and similar patterns
qznc
qznc at web.de
Wed Dec 4 22:24:50 PST 2013
On Thursday, 5 December 2013 at 01:07:19 UTC, Timothee Cour wrote:
> A1.
>
> Is there a (clever?) way to achieve the following using a
> single function
> call?
You could (mis)use destructors.
=============
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.
More information about the Digitalmars-d-learn
mailing list