Hiding class pointers -- was it a good idea?

Russell Lewis webmaster at villagersonline.com
Wed Aug 15 12:49:51 PDT 2007


Tristam MacDonald wrote:
> Russell Lewis Wrote:
>> But the worst of it all, IMHO, is what you (and others) have pointed 
>> out: the lack of clarity about whether a declaration is a 
>> struct-by-value or class-by-reference.  That means that if a container 
>> was originally implemented as a struct, you can't ever change it to a 
>> class, since that would require rewriting all of the code that uses it. 
>>   Doesn't that violate one of the basic principles of Object Orientation 
>> - the hiding of implementation?

Tristam, your response felt a little "hotter" than I what I prefer.  If 
I sounded a little flamish in my post, I apologize.  If I'm misreading 
you, I also apologize!

> I would tend to disagree. Classes are reference types, structs are value types. When I refactor something from a class to a struct, I *expect* the behavior of function arguments to switch from by-refrence to by-value.

I think that you made my point for me...if refactoring requires lots of 
rewrites, then the use-side of the code has too much insight into the 
implementation.  I don't see any fundamental reason why classes need to 
be reference types, other than history.  (Yes, they are currently all on 
the heap, even scope variables, but even that is a decision that I could 
change, if there was a reason for it.)

That's MHO, anyhow.

> Furthermore, since classes *cannot* be passed by value, it makes no sense to have additional syntax, which will lead to begginers writing 'Object o;' instead of 'Object* o;' - which will not compile under your suggestion.

What's wrong with
	Object o;
?  If I had a magic wand, I would declare that syntax to be equivalent 
to the current syntax:
	scope Object o = new Object;
or, better, to make it a stack variable.


So the current implementation requires "extra syntax" as well (for scope 
declarations).  It's a question of which extra syntax is better.  IMHO, 
making classes consistent with structs is more desirable than 
eliminating a few stray asteriskes.  The beginners who stumble across 
the "can't pass a class as a by-value function parameter" rule can be 
easily educated with a well-written error message emitted by the compiler.


I don't have a magic wand, and I don't want everybody to have to rewrite 
their old code.  But I continue to hope that this might change in 2.0, 
or maybe 3.0. :)



More information about the Digitalmars-d mailing list