[Issue 16056] New: [The D Bug Tracker]

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun May 22 01:49:55 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=16056

          Issue ID: 16056
           Summary: [The D Bug Tracker]
           Product: D
           Version: D2
          Hardware: All
               URL: http://dlang.org/
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P3
         Component: dlang.org
          Assignee: nobody at puremagic.com
          Reporter: eyal.lotem at gmail.com

The "immutable" qualifier is not transitive, as it claims to be.

import std.stdio;

struct Foo {
    int y;
    void delegate() pure f;
}

pure void pure_func(immutable Foo foo)
{
    foo.f();
}

void main() {
    int y;
    immutable x = Foo(1, { y++; });
    writeln("Before: ", y);
    pure_func(x);
    writeln("After: ", y);
}

Prints out:
Before: 0
After: 1

A pure function with only immutable parameters should have no possible
side-effects. However, due to 'immutable' not transitively applying to
delegates' context:

A) We can have a mutable-context delegate inside an immutable struct
B) A pure function taking exclusively immutable parameters is not really pure

The fix would be to transitively apply the "immutable" qualifier to the context
type of the Foo.f delegate.

--


More information about the Digitalmars-d-bugs mailing list