www.digitalmars.com         C & C++   DMDScript  

D - RAIIi, auto, classes, structs

reply "Craig Black" <cblack ara.com> writes:
Maybe I'm just missing something here, but I could never figure out why it
is necessary to have classes and structs.  The behavior of C++ classes and
structs is almost identical.  In C++ you can declare classes (or structs) to

different approach where structs are value types on the stack and classes
are reference types.  If I understand correctly,  D does something similar



fact that C++ "new" does not return a reference counted garbage-collected

If you want a data type to be used as a value type _and_ a reference type,
you are required to declare both a class and a structure, whose contents are
identical.  We edumecated folks call this sort of thing code duplication.



Java, a language that does not support "value types".

/////////////////////////////////
C++ sytax.

stack variable:
ClassName variableName;

heap variable:
ClassName *varName = new ClassName;

//////////////////////////////////////
D syntax ?

I'm not sure I completely grasp the D's appraoch.  Are D's heap objects
reference counted objects?  If I am not mistaken "raii" or "auto" allows the
programmer to use classes as automatic variables.  Correct me if I
misunderstand.

stack variable:
auto ClassName varName = new ClassName;

heap variable ?reference counted?:
ClassName varName = new ClassName;

///////////////////////////////////////
My Proposition for D:

I propose we ditch the whole class, struct approach to reference and value
types.
I think we should allow classes to be either, but not with the above syntax.
Everything in Java is a reference type, so there was no need to say "This is
a reference."  However, in a language that supports both reference and value
types, I believe we should use a different syntactical approach.

stack variable:
ClassName varName;

heap variable (reference counted):
ref ClassName varName = new ClassName;
-or-
ClassName& varName = new ClassName;

Thus, there is no need for an "raii" or "auto" keyword.
"struct" is also unnecessary, since a class can be a value or reference
type.

Also, it should not be necessary to differentiate between a reference and
value type function parameter.  You should be able to pass in either one as
a parameter to a function or method.
This is because the language should automatically pass all objects by
reference anyway, since it is usually more efficient.  However, in such a
situation an "in" parameter should not be allowed to be modified by a
function.

Please give me some feed back on this idea.

-Craig
Aug 28 2002
next sibling parent reply "Craig Black" <cblack ara.com> writes:
 Also, it should not be necessary to differentiate between a reference and
 value type function parameter.  You should be able to pass in either one
as
 a parameter to a function or method.
 This is because the language should automatically pass all objects by
 reference anyway, since it is usually more efficient.  However, in such a
 situation an "in" parameter should not be allowed to be modified by a
 function.
Er, wait a sec, I'm wrong here, but not completely. You should be able to pass in either a value or reference type into any function parameter. However, you should be able to specify a parameter as a reference type, if you wanted to assign it to another reference. For example: class A { ... } void foo1(A a) {...} void foo2(ref A a) { ref A b = a; // create another reference to the object ... } void bar() { A val; // value type ref A = new A; // ref type foo1(val); // ok foo1(a); // ok foo2(val); // illegal foo2(a); // ok }
Aug 28 2002
parent "Craig Black" <cblack ara.com> writes:
Let me fix my typos for that example:

 class A { ... }

 void foo1(A a) {...}

 void foo2(ref A a)
 {
 ref A b = a;  // create another reference to the object
 ...
 }

 void bar()
 {
   A valVar; // value type
   ref A refVar = new A;  // ref type

   foo1(valVar);  // ok
   foo1(refVar);    // ok
   foo2(valVar);  // illegal
   foo2(refVar);    // ok
 }
Aug 28 2002
prev sibling parent "Walter" <walter digitalmars.com> writes:
In C++, classes and structs are essentially indistinguishable. In D, struct
types are value types and class types are reference types. The classes are
for all the object oriented stuff, and the structs are for C compatibility
and for lightweight stack objects.

No, the storage allocation is not reference counted.

Eliminating the value semantics for object oriented classes chucks a whole
lot of complexity that C++ has.
Aug 28 2002