|  | 
  | Posted by d-bugmail |  Permalink Reply |  
  | 
d-bugmail  
 
 
  | 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...
-- 
 |