Using D

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Sun Jul 13 04:50:21 PDT 2014


On 2014-07-12 05:59, Rikki Cattermole wrote:

> Something I've been thinking about is an overload for with statement.
> E.g.
>
> with(new MyAllocator) {
>      void*[256] values;
>      //...
> }
>
> class MyAllocator : IGC {
>      private {
>          IGC prevGC;
>      }
>
>      void opWithIn() {
>          this.prevGC = GC.getImpl();
>          GC.setImpl(this);
>      }
>
>      void opWithOut() {
>          GC.setImpl(this.prevGC);
>      }
> }

Or without language changes:

void withAllocator (alias allocator, alias block)
{
     auto prevGC = GC.getImpl();
     scope(exit)
         GC.setImpl(this.prevGC);

     GC.setImpl(allocator);
     block();
}

withAllocator!(new Allocator, {
     void*[256] values;
});

Not as nice syntax though. That could of course be fixed with AST macros 
[1] :)

[1] http://wiki.dlang.org/DIP50#Statement_Macros

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list