digitalmars.D.learn - Read only delegate
- Edwin van Leeuwen (19/19) Apr 04 2016 Is there a way to make sure a delegate only reads state, without
- Edwin van Leeuwen (21/24) Apr 04 2016 Note that annotating with pure also doesn't help. As a result we
- Rene Zwanenburg (4/8) Apr 04 2016 Yeah this is a nasty old issue. The underlying problem is that a
- Kagamin (4/5) Apr 04 2016 Bug 1983 is about usage of delegates after creation, restrictions
- Edwin van Leeuwen (8/15) Apr 04 2016 I think the underlying issue is the same. The problem seems to be
- Edwin van Leeuwen (3/13) Apr 04 2016 Thanks for the reference, hopefully this will be resolved at some
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
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
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
On Monday, 4 April 2016 at 11:32:23 UTC, Rene Zwanenburg wrote:https://issues.dlang.org/show_bug.cgi?id=1983Bug 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
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:I think the underlying issue is the same. The problem seems to be that:https://issues.dlang.org/show_bug.cgi?id=1983Bug 1983 is about usage of delegates after creation, restrictions during creation are enforced. AIU, OP wants to have const check during creation.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
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:Thanks for the reference, hopefully this will be resolved at some point :)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