Thread overview
Read only delegate
Apr 04, 2016
Edwin van Leeuwen
Apr 04, 2016
Edwin van Leeuwen
Apr 04, 2016
Rene Zwanenburg
Apr 04, 2016
Kagamin
Apr 04, 2016
Edwin van Leeuwen
Apr 04, 2016
Edwin van Leeuwen
April 04, 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]
}
```


April 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
}
```

April 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
April 04, 2016
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.
April 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:
>> 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 :)

April 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:
>> 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.