www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Read only delegate

reply Edwin van Leeuwen <edder tkwsping.nl> writes:
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]
}
```
Apr 04 2016
next sibling parent Edwin van Leeuwen <edder tkwsping.nl> writes:
On Monday, 4 April 2016 at 08:10:10 UTC, Edwin van Leeuwen wrote:
 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.
Note that annotating with pure also doesn't help. As a result we can have a pure delegate that returns different results every time it is called. ```D void main() { import std.stdio : writeln; auto r = [0,1,2,3]; auto f = delegate() const pure { import std.array : front, popFront; r.popFront; return r.front; }; r.writeln; // [0,1,2,3] auto f1 = f(); r.writeln; // [1,2,3] assert( f() == f1 ); // Throws } ```
Apr 04 2016
prev sibling parent reply Rene Zwanenburg <renezwanenburg gmail.com> writes:
On Monday, 4 April 2016 at 08:10:10 UTC, Edwin van Leeuwen wrote:
 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.
 ```
Yeah this is a nasty old issue. The underlying problem is that a delegate's function and context pointers are completely untyped. https://issues.dlang.org/show_bug.cgi?id=1983
Apr 04 2016
next sibling parent reply Kagamin <spam here.lot> writes:
On Monday, 4 April 2016 at 11:32:23 UTC, Rene Zwanenburg wrote:
 https://issues.dlang.org/show_bug.cgi?id=1983
Bug 1983 is about usage of delegates after creation, restrictions during creation are enforced. AIU, OP wants to have const check during creation.
Apr 04 2016
parent Edwin van Leeuwen <edder tkwsping.nl> writes:
On Monday, 4 April 2016 at 11:39:55 UTC, Kagamin wrote:
 On Monday, 4 April 2016 at 11:32:23 UTC, Rene Zwanenburg wrote:
 https://issues.dlang.org/show_bug.cgi?id=1983
Bug 1983 is about usage of delegates after creation, restrictions during creation are enforced. AIU, OP wants to have const check during creation.
I think the underlying issue is the same. The problem seems to be that:
 Unfortunately, there is no way to declare a const delegate (by 
 which I mean, a delegate whose context pointer is typed const).
I actually discovered the problem, due to the hole it leaves in the const system, where I got different results calling a const method multiple times. The const method in question called a delegate that changed its context pointer, resulting in changes during calls.
Apr 04 2016
prev sibling parent Edwin van Leeuwen <edder tkwsping.nl> writes:
On Monday, 4 April 2016 at 11:32:23 UTC, Rene Zwanenburg wrote:
 On Monday, 4 April 2016 at 08:10:10 UTC, Edwin van Leeuwen 
 wrote:
 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.
 ```
Yeah this is a nasty old issue. The underlying problem is that a delegate's function and context pointers are completely untyped. https://issues.dlang.org/show_bug.cgi?id=1983
Thanks for the reference, hopefully this will be resolved at some point :)
Apr 04 2016