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