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

Reiner Pope reiner.pope at REMOVE.THIS.gmail.com
Sun Sep 10 13:48:22 PDT 2006


Kristian wrote:
> On Sun, 10 Sep 2006 18:59:34 +0300, Sean Kelly <sean at f4.ca> wrote:
>> Kristian wrote:
>>> And, I hope that D will have a 'const' type specifier in the future. 
>>> Now you cannot know if a function will modify an object that is 
>>> passed to it as a parameter. Or if a member function of a class will 
>>> change the internal status of the object.
>>
>> Don't we all.  But wanting it and coming up with a solid proposal for 
>> how it should work are entirely different issues, unfortunately.
> 
> Well, what's wrong with the way C++ does it?
> 
> class Obj {
>     void f() const;
>     void g();
> 
>     int v;
> }
> 
> void func(const Obj o) {
>     o.f();    //ok
>     o.g();    //error
>     o.v = 1;  //error
> }

Do some searching for 'const' and 'readonly' on this NG, and you should 
get the idea:
  - C++ const is ugly, so people don't use it enough;
  - const_cast removes the safety guarantees of const, making 
const-directed optimizations by the compiler impossible.
  - overloading by const requires duplication of code.
  - a trivial const system like C++ is too strict and may require 
unnecessary clones to avoid a static const violation, thus necessitating 
the need for const_cast
  - reference immutability (C++'s 'readonly view') is only one form of 
const, and can't guarantee other (important) immutability contracts
  - after such a problems, the merits are not so clear, since the 
usefulness can only really be attributed to two things:
     1. Standardising documentation of variable usage.
     2. Getting the compiler to help catch programmer errors.
   and some people claim that they have never found any bugs by using const.

Cheers,

Reiner



More information about the Digitalmars-d mailing list