A template construct like using()

Chris Katko ckatko at gmail.com
Tue Apr 26 21:33:43 UTC 2022


I swear I asked something like this before years ago but it 
doesn't show up in my previous forum posts.

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.

Perhaps a mixin is what I'm looking for?


More information about the Digitalmars-d-learn mailing list