classes structs

David Currie curriedr at iinet.net.au
Mon Sep 17 22:07:43 PDT 2012


On Saturday, 15 September 2012 at 10:58:07 UTC, Jonathan M Davis 
wrote:
> Classes are polymorphic. They have inheritance and virtual 
> functions.
> Polymorphism makes no sense with a variable on the stack. 
> Having inheritance
> with objects on the stack risks object slicing (
> http://en.wikipedia.org/wiki/Object_slicing ). Sure, you _can_ 
> have a
> polymorphic object which is on the stack (which is why C++ 
> allows classes on
> the stack and why D has std.typecons.scoped), but it's the sort 
> of thing that
> tends to be begging for bugs.
>
> The designers of D decided that it's was cleaner and safer to 
> separate objects
> which were meant to be polymorphic and those which were meant 
> to be non-
> polymorphic (as is often considered best practice in C++), and 
> since having
> polymorphic objects means that you're not using their 
> polymorphism and having
> them on the stack can be dangerous, it was decided to follow 
> Java and C#'s
> example and make all classes into reference types which live on 
> the heap
> (though unlike Java and C#, we _can_ put such objects on the 
> stack if we
> really want to via std.typecons.scoped). But unlike Java and 
> C#, we have
> structs which are full-on objects with constructors and 
> destructors and are
> the same in classes in pretty much every way except that they 
> have no
> polymorphism and normally go on the stack.
>
> The result is safer than what C++ has but is still very 
> powerful. And since
> it's arguably best practice not to put objects which are meant 
> to use
> polymorphism on the stack in C++ anyway, it's not even really 
> restricting you
> from much in comparison to C++ (and std.typecons.scoped makes 
> it possible to
> put classes on the stack if you really want to, making the 
> restrictions even
> less).
>
> You can like it or not, but separating structs and classes and 
> making classes
> reference types on the heap is a design decision based on the 
> best practices
> and common bugs in C++. And it works very well. Upon occasion, 
> it can be
> limiting (hence why we have std.typecons.scoped), but I don't 
> think that
> you're going to find very many D programmers who think that the 
> separation of
> structs and classes was a bad idea.
>
> You should keep in mind that D's general philosophy is to make 
> the defaults
> safe but to allow you to do more powerful, dangerous stuff when 
> you need to.
> The result is that it's just as powerful as C++ when you need 
> it to be but
> that it's a lot safer in general, meaning that you're going to 
> have fewer bugs
> in your code.
>
> A prime example of this is the fact that all variables are 
> default-
> initialized. This way, you never have problems with variables 
> being
> initialized to garbage, which can cause non-deterministic, 
> hard-to-track-down
> bugs. But if you really need the extra speed of a variable not 
> being
> initialized when it's declared, then you can initialize it to 
> void. e.g.
>
> int i = void;
>
> This makes it so that code is far less error-prone in general 
> while still
> allowing you to have the same down to the metal speed that 
> C/C++ offers when
> you really need it. And it's that philosophy which governs a 
> lot of D's
> features.
>
> - Jonathan M Davis

I have SO MANY issues with the above statements I wouldnt know 
where to start.
I have been attempting to raise these (and many related) issues 
before.

Firstly, responding in particular to Jonathan M Davis (albeit 
rather late),
I concede your comments are made in good faith and are even 
ACCURATE.
My problem is that they are INSUFFICIENT.

I haven't announced to regular viewers that I STARTED these 
issues by writing direct to Walter (twice) that I had some 
language extension IDEAS I thought would be VALUABLE to ALL. Such 
Ideas would make a welcome addition to ANY language but for me D 
comes closest.
(A C like language with NO header files alone is worth 50%).
Naturally all he could really do was to send me along to the 
forums.

However, I can see that if I am to receive the usual polite 
refusal,
and I really wish to make a POINT I'd better be armed.

I would need to be able to speak your language (learn D)
before I should expect you to speak mine (implement My Ideas).
I can see here that my best bet is to learn D.

Can one learn it all online?  What is the best D book?



More information about the Digitalmars-d mailing list