Hiding class pointers -- was it a good idea?
Russell Lewis
webmaster at villagersonline.com
Wed Aug 15 11:10:13 PDT 2007
I agree, IMHO this was a mistake. With automatic type deduction, the
lack of '*' doesn't really save any typing anymore, at least for
declarations, which (I suspect) are by far the most common use:
auto temp = new MyClass;
It saves (a tiny bit of) typing on function declarations, and avoids the
need for a copy constructor, but we could have eliminated copy
constructors simply by making pass-by-value class arguments to functions
illegal.
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?
Russ
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.
>
> Bad points:
> - Harder to tell whether you're dealing with a pointer or not
> (c.f. the common uninitialized 'MyObject obj;' bug)
> - To build on the stack, have to use 'scope'
>
> Good points:
> + No need to type '*' everywhere when you use class objects
> + ??? anything else ???
>
>
> I guess I feel like that if D were Java, I'd never see the pointers,
> "Foo foo;" would always mean a heap-allocated thing, and everything
> would be cool. But with D sometimes "Foo foo" means a stack allocated
> thing, and sometimes it doesn't. I'm starting to think the extra
> cognitive load isn't really worth it.
>
> 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. Classes can now be stack-allocated with
> scope. Structs are supposedly going to get constructors, and maybe even
> destructors. It kind of makes me think that maybe separating structs
> and classes was a bad idea. I have yet to ever once say to myself
> "thank goodness structs and classes are different in D!", though plenty
> of times I've muttered "dangit why can't I just get a default copy
> constructor for this class" or "shoot, these static opCalls are
> annoying" or "dang, I wish I could get a weak reference to a struct", etc.
>
> --bb
More information about the Digitalmars-d
mailing list