Hiding class pointers -- was it a good idea?

Sean Kelly sean at f4.ca
Wed Aug 15 13:24:44 PDT 2007


Bill Baxter wrote:
> I'm starting to seriously wonder if it was a good idea to hide the 
> pointers to classes.  It seemed kinda neat when I was first using D that 
> I could avoid typing *s all the time.  But as time wears on it's 
> starting to seem more like a liability.

Funny.  I started out having concerns about the inconsistency here and 
have decided it's a good thing :-)  I think the salient point is that 
classes in D do not and will never (as far as I know) have a value 
type--they are always a reference type, even when "scope" is used. 
However, requiring the user to supply an asterisk for all class variable 
declarations suggests to me that I can remove the asterisk and get a 
value type, which I cannot.  Back before the addition of "scope" I'd 
thought the syntax of scoped objects would be more like a value type:

MyClass c = MyClass();

in which case I think the presence of an asterisk may have been more 
justified, though the lack of pass-by-value would likely still have 
rendered it too confusing to be practical IMO.

I think in D we just have to accept that classes are special and deserve 
special syntax.

> In large part it all stems from the decision to make classes and structs 
> different.  That seems nice in theory, but as time goes on the two are 
> becoming more and more alike.

Personally, I think structs and classes are nothing alike in D and I 
consider this a good thing.  One of my pet peeves about C++ is that it 
has two keywords to represent the same darn thing.

The distinction between structs and classes really has more to do with 
how they are intended to be used than how they look.  Structs are simply 
simple aggregate data types that are allowed to contain functions for 
convenience.  In fact, I think the need for a copy ctor is debatable, 
given the purpose of structs (even though I've wanted one in the past), 
as is the need for a dtor.  Real ctor support seems entirely reasonable, 
however, because everything needs to be initialized, and ctors make this 
both convenient and efficient (the static opCall approach is a confusing 
hack).


Sean



More information about the Digitalmars-d mailing list