D's greatest mistakes

Steven Schveighoffer schveiguy at yahoo.com
Mon Nov 29 08:12:00 PST 2010


On Mon, 29 Nov 2010 10:54:04 -0500, Nick Sabalausky <a at a.a> wrote:

> "Steven Schveighoffer" <schveiguy at yahoo.com> wrote in message
> news:op.vmxqi4kmeav7ka at steve-laptop...
>> On Sun, 28 Nov 2010 23:19:44 -0500, Jack <jt at overlook.biz> wrote:
>>
>>> The post "C#'s greatest mistakes" prompts/begs this post. Have at it,
>>> pick up the ball and run with it, don't be shy. I expect Walter and
>>> Andrei to answer (if Walter and Andrei so dare!) after others' posts  
>>> have
>>> stopped or stagnated into that cesspool of threaded discussion that is
>>> "the subthread" or "tangential thread" (which surely needs a rock
>>> anthem).
>>
>> As I understand this is a troll post, but still provoked some good
>> discussion, I'll throw in my biggest problem with D:
>>
>> lack of tail-const/immutable for classes.
>>
>
> I've heard this before, but I'm a little unclear. Can you give an  
> example?


This is a tail-const pointer:

const(int)* ptr;

I can reassign ptr at will:

ptr = &x;
ptr = &y;

but I can't change what ptr points to:

*ptr = 5; // error

Because classes are references, just like pointers, I would like a way to  
have a tail-const class reference.  But it's not possible:

const(C) myref;

myref = a; // error, cannot change const myref

You can have a tail-const pointer to a class reference:

C myref;
const(C) *myrefptr = &myref;

But a class is on the heap, and the reference 'myref' is on the stack, so  
myrefptr is only usable locally.  In order to keep the 'infinite' lifetime  
property, I have to create a class reference on the heap, and then return  
a pointer to *that*.  Just try to create a class reference on the heap.

Not only that, but I now have to waste 16 bytes of heap space just to have  
a tail-const class reference, *just because* the syntax is incomplete.  I  
find this unacceptable.

-Steve


More information about the Digitalmars-d mailing list