Thread overview
[Issue 2027] New: No way to declare only the reference 'const' or 'invariant' for reference types.
Apr 24, 2008
d-bugmail
Apr 24, 2008
Janice Caron
May 15, 2008
d-bugmail
April 24, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2027

           Summary: No way to declare only the reference 'const' or
                    'invariant' for reference types.
           Product: D
           Version: 2.012
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: htvennik@zonnet.nl


While using D I often want a reference to a class object to be invariant, while the object itself should be mutable. Or, in my current case, I want a delegate variable to be invariant (i.e. the reference may not change), but I do not want that only a invariant function may be assigned to it on initialization...

This is obviously an undesirable limitation of the D 2.0 type system. Probably there should be some storage class for these cases.

The following piece of code demonstates the problem:

class A {
    private:
        invariant Object o_;

    this(Object o) {
        o_ = o;        // error, cannot implicitly convert to invariant
    }
}

Because o_ is of type invariant(Object), it cannot be assigned a value of type Object, which is perfecly correct. But I do not intend o_ to be of type invariant(Object), I only want o_ to reference the same instance of Object, throughout the lifetime of the instance of class A, and thus forbid re-assignment...


-- 

April 24, 2008
That's not a bug, that's a request for head-const. :-)
May 15, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2027


bugzilla@digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #2 from bugzilla@digitalmars.com  2008-05-14 19:07 -------
Const and invariant are designed to be fully transitive. There is no way to declare a const reference to mutable data; this is by design. (It has been debated very extensively in the newsgroups.)


--