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

Kristian kjkilpi at gmail.com
Sun Sep 10 05:29:02 PDT 2006


I'm used for having both the variable types, 'physical' and  
reference(/pointer) ones, so it feels strange to work with D (or Java,  
etc) that have reference types only.

First, there is no way to copy/clone an object (in general). That is,  
unless you provide a copy/clone function (for all the classes you're  
using). Second, you cannot create classes that behave like basic types,  
e.g. like integers.

For example, in C++ you could have:

void func() {
     Coord a, b, c;

     a.set(1, 2);
     b.set(5, 10);
     c = a + b;  //'c' is (6, 12)
     b = c;
     b.setX(2);  //'c' is unchanged, 'b' is (2, 12)
}

The 'Coord' objects have no 'global meaning' here, they are just simple  
values used in calculations. (So there is no need to reference them from  
other parts of the code.)

I think it's a big restriction if a language does not have both the  
variable types (physical + references).
Why D and other similar languages have no physical types?


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.

When these things are summed together, I'm afraid that I (at least) will  
create a number of situations where the values of objects are incorrectly  
changed in almost any part of the code. Hopefully not, though.



More information about the Digitalmars-d mailing list