Read only delegate

Edwin van Leeuwen via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 4 01:10:10 PDT 2016


Is there a way to make sure a delegate only reads state, without 
changing it? I tried annotating the delegate as const, but that 
does not seem to work.

```D
void main()
{
     import std.stdio : writeln;
     auto r = [0,1,2,3];

     auto f = delegate() const // Compiles even though we are 
changing r
     {
         import std.array : popFront;
         r.popFront;
     };

     r.writeln; // [0,1,2,3]
     f();
     r.writeln; // [1,2,3]
}
```




More information about the Digitalmars-d-learn mailing list