[Issue 4714] New: Cannot return ref this when struct has invariant

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Aug 22 14:41:33 PDT 2010


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

           Summary: Cannot return ref this when struct has invariant
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: jmdavisProg at gmail.com


--- Comment #0 from Jonathan M Davis <jmdavisProg at gmail.com> 2010-08-22 14:41:29 PDT ---
The following code

import std.stdio;

struct S
{
    ref S hello()
    {
        writeln("hello");

        return this;
    }

    invariant()
    {
    }
}

void main()
{
    S().hello().hello().hello();
}


results in the error

prog.d(9): Error: __result = this is not mutable
prog.d(9): Error: __result = this is not an lvalue


If I remove the invariant, it compiles and runs just fine. Also, if I compile
with -release, it compiles and runs just fine. But if that invariant is there
and compiled in (as it would be without -release), it won't compile. The same
goes for out if I use it instead of invariant.

This makes it very difficulty to use the idiom where you have member functions
returning references to the object so that you can chain function calls. That
is, it's impossible to use that idiom and use invariants or out contracts. I
assume that something in the invariant and out code dealing with the return
value does not take into account the fact that you could return a reference to
this from a struct's member function.

-- 
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