Using D

Rikki Cattermole via Digitalmars-d digitalmars-d at puremagic.com
Sun Jul 13 05:01:29 PDT 2014


On 13/07/2014 11:50 p.m., Jacob Carlborg wrote:
> 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

Definitely, but there is a rather big difference in requirements to 
implement them ;)
But in saying this, we might be able to move the with statement into 
druntime via AST macros. Should it have the ability to modify the this 
context like with statement does now.



More information about the Digitalmars-d mailing list