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

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

import std.stdio;

struct S
    ref S hello()

        return this;


void main()

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:
------- You are receiving this mail because: -------

Reply via email to