Bug 2027 – No way to declare only the reference 'const' or 'invariant' for reference types.
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2008-04-24T11:58:00Z
Last change time
2015-06-09T01:14:36Z
Assigned to
bugzilla
Creator
htvennik
Comments
Comment #0 by htvennik — 2008-04-24T11:58:50Z
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...
Comment #1 by caron800 — 2008-04-24T12:36:50Z
That's not a bug, that's a request for head-const. :-)
Comment #2 by bugzilla — 2008-05-14T19:07:44Z
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.)