Why there are no 'physical' object variables (only references)?

Reiner Pope reiner.pope at REMOVE.THIS.gmail.com
Wed Sep 13 02:02:27 PDT 2006


Steve Horne wrote:
> On Tue, 12 Sep 2006 17:49:44 +0300, Kristian <kjkilpi at gmail.com>
> wrote:
> 
>> C++ const is better than nothing. Hopefully there will be a solution that  
>> will suit everybody, more or less.
Lack of const and requiring people to document their code well is the 
only painless solution at the moment, I think. That, and switching to a 
pure functional language.
> 
> But I have mixed feelings about C++ const. It has caught errors for
> me, but not often, and quite often those errors wouldn't exist without
> 'const' anyway. 
How can you call that catching errors?

> Of course I understand the efficiency benefits, but it's not very
> intuitive.
As Walter says, the efficiency benefits are very small because of the 
aliasing problem and the presence of const_cast. The main two reasons 
for const are catching errors and documenting code.

> Pointers even need to be told whether it is the pointer that is const
> or the value pointed to, or both for that matter.
> 
This is what I was talking about with the ugliness of const, not the 
naming system.

   const Fred const * p;

is a bit excessive, don't you think? Not to mention very illegible. D's 
move to implicit reference semantics for classes means that we can get a 
  generally-easier-to-read syntax, like is proposed in Javari:

   readonly final Fred p;

where 'readonly' refers to the actual data, and 'final' means that p 
can't be used as an lval (ie, the pointer itself is const).


> So the dependencies mean you have three options...
> 
> 1.  Make constness into something you think about all the time.
> 2.  Don't do constness at all.
> 3.  Do half-hearted constness and have loads of const-casts,
>     defeating the point.
> 
> And then, your code has to work with someone elses... Someone who has
> a very different approach.
In a way, the choice is made on a language level, which is the concern 
about const in D. As you have described, it becomes a real nuisance in 
C++, and the benefits are not always clear.

Just giving a taste of the discussions of const on this newsgroup... <g>

Cheers,

Reiner



More information about the Digitalmars-d mailing list