On Tue, Apr 26, 2022 at 09:33:43PM +0000, Chris Katko via Digitalmars-d-learn wrote: [...] > I'm looking for a construct that mimics using(var)/with(var) > > ````D > bitmap* b; > > draw_with(b) > { > draw_pixel(red, 16, 16); //draw red pixel to bitmap b (b is implied above) > } > ```` > > But the code ends up being: > ````D > bitmap* b; > > set_target_bitmap(b); //entry code > draw_pixel(red, 16, 16); // body > set_target_bitmap(original_target); // exit code > ```` > > The essence is wrapping something the code up in a kind of RAII-like > entry and exit code that references a given target variable. [...]
Kinda ugly-looking, but here's the first idea that came to mind: bitmap* b; { set_target_bitmap(b); scope(exit) set_target_bitmap(original_target); draw_pixel(...); ... // blah blah blah } // scope(exit) runs here T -- Unix was not designed to stop people from doing stupid things, because that would also stop them from doing clever things. -- Doug Gwyn