Thread overview
Re: scoped chdir and similar patterns
Dec 05, 2013
Jonathan M Davis
Dec 05, 2013
Timothee Cour
Dec 05, 2013
Jonathan M Davis
December 05, 2013
On Wednesday, December 04, 2013 17:07:03 Timothee Cour wrote:
> A1.
> 
> Is there a (clever?) way to achieve the following using a single function
> call?
> 
> //does chdir
> 
> void fun(){
> ...
> string dir0=getcwd; scope(exit) chdir(dir0); chdir(dir);
> ...
> }
> 
> //desired:
> void fun(){
> ...
> chdir_scoped(dir);
> ...
> }

Sounds like a job for RAII. e.g.

{
    auto tcd = TempCD(dir);
    // do stuff
} //tcd leaves scope and its destructor resets the cwd to what it was

In general, I'd say that scope statements are for situations where you don't have to do the same thing in very many places, whereas RAII is better when you have to do the exact same thing in several places.

- Jonathan M Davis
December 05, 2013
RAII, of course! thanks!
btw, what about A3 above?


On Wed, Dec 4, 2013 at 5:14 PM, Jonathan M Davis <jmdavisProg@gmx.com>wrote:

> On Wednesday, December 04, 2013 17:07:03 Timothee Cour wrote:
> > A1.
> >
> > Is there a (clever?) way to achieve the following using a single function
> > call?
> >
> > //does chdir
> >
> > void fun(){
> > ...
> > string dir0=getcwd; scope(exit) chdir(dir0); chdir(dir);
> > ...
> > }
> >
> > //desired:
> > void fun(){
> > ...
> > chdir_scoped(dir);
> > ...
> > }
>
> Sounds like a job for RAII. e.g.
>
> {
>     auto tcd = TempCD(dir);
>     // do stuff
> } //tcd leaves scope and its destructor resets the cwd to what it was
>
> In general, I'd say that scope statements are for situations where you don't have to do the same thing in very many places, whereas RAII is better when you have to do the exact same thing in several places.
>
> - Jonathan M Davis
>


December 05, 2013
On Wednesday, December 04, 2013 17:38:36 Timothee Cour wrote:
> RAII, of course! thanks!
> btw, what about A3 above?

Create an enhancement request for it:

https://d.puremagic.com/issues

And of course, if you really want it implemented, you can always just do it yourself and create a pull request for it. I don't know how Lars or any of the other folks who would be very opinionated about std.process would feel about such a change, but if you have a good argument for why it would make sense, then I'd think that there would be a good chance of it being accepted.

- Jonathan M Davis