[Issue 4838] Cannot declare a delegate variable for const member functions

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri May 11 03:08:19 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=4838


SHOO <zan77137 at nifty.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |zan77137 at nifty.com


--- Comment #9 from SHOO <zan77137 at nifty.com> 2012-05-11 03:09:41 PDT ---
This bug is very important for const/shared/immutable correctness.
The following code is caused by this issue clearly:

-------------------------
class A {
    int x;
    void foo() { x = 1; }
}

void bar(void delegate() dg) {
    dg();
}

void main() {
    immutable a = new immutable(A);
    assert(a.x == 0);
    bar(&a.foo);       // This line should be compilation error.
    assert(a.x == 0);  // core.exception.AssertError at main(14): Assertion
failure
}
-------------------------

Its workaround is the following code:

-------------------------
class A {
    int x;
    void foo() { x = 1; }
}

alias typeof(&(new class(){void foo() const{}}).foo) void_delegate_const;
void bar(void_delegate_const dg) {
    dg();
}

void main() {
    immutable a = new immutable(A);
    assert(a.x == 0);
    bar(&a.foo);      // main.d(14): Error: function main.bar (void delegate()
const dg) is not callable using argument types (void delegate())
    assert(a.x == 0);
}
-------------------------

It is painful to write such correct code.
Fortunately, there is a PullRequest, so I hope to be merged as soon as
possible.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list