www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - The constness problem

reply "Peter Alexander" <peter.alexander.au gmail.com> writes:
http://pointersgonewild.wordpress.com/2014/07/11/the-constness-problem/

I can relate to this. I don't use classes much in D, but I find 
it frustrating that you need to reach to std.typecons.Rebindable 
for what I would consider to be a more common usage of const with 
classes.

I really feel that a mutable reference to a const object should 
be expressible in the core language.
Jul 12 2014
next sibling parent "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Sat, Jul 12, 2014 at 03:03:12PM +0000, Peter Alexander via Digitalmars-d
wrote:
 http://pointersgonewild.wordpress.com/2014/07/11/the-constness-problem/
 
 I can relate to this. I don't use classes much in D, but I find it
 frustrating that you need to reach to std.typecons.Rebindable for what
 I would consider to be a more common usage of const with classes.
 
 I really feel that a mutable reference to a const object should be
 expressible in the core language.
+1. This also adversely affects generic code. As soon as you have a const in there, things break. We badly, badly, need tail-const. T -- Let X be the set not defined by this sentence...
Jul 12 2014
prev sibling parent "Lars T. Kyllingstad" <public kyllingen.net> writes:
On Saturday, 12 July 2014 at 15:03:13 UTC, Peter Alexander wrote:
 http://pointersgonewild.wordpress.com/2014/07/11/the-constness-problem/

 I can relate to this. I don't use classes much in D, but I find 
 it frustrating that you need to reach to 
 std.typecons.Rebindable for what I would consider to be a more 
 common usage of const with classes.
It seems she laments the lack of both head-const in general and tail-const for classes. Head-const isn't really an issue, in my opinion, because it is rarely needed and easily obtained with existing language features: struct HeadConst(T) { this(T value) { value_ = value; } alias get this; private: property T get() { return value_; } T value_; } HeadConst!MyClass obj = new MyClass; obj = new MyClass; // Error! The lack of tail-const class references, on the other hand, is one of D's most severe shortcomings.
 I really feel that a mutable reference to a const object should 
 be expressible in the core language.
Michel Fortin once made a working pull request for DMD that introduced the syntax const(MyClass) ref thisIsRebindable = new MyClass; Personally, I would prefer replacing the current object reference syntax with T$ (or T# if that conflicts with the current dollar operator), like this: Foo$ obj1 = new Foo; // Rebindable and mutable const(Foo)$ obj2 = new Foo; // Rebindable but const const(Foo$) obj3 = new Foo; // Fully const The current 'T' reference type would of course have to be accepted and equivalent to the 'T$' reference type for a long time before it is eventually deprecated. Come to think of it, "new T" could return "T$" for non-class types too, where T$ would then act like a non-dereferentiable pointer on which it is not allowed to do pointer arithmetics. Lars
Jul 12 2014