Hiding class pointers -- was it a good idea?

James Dennett jdennett at acm.org
Thu Aug 16 22:48:09 PDT 2007


Walter Bright wrote:
> James Dennett wrote:
>> Walter Bright wrote:
>>> Value types are fundamentally different from reference types. D gives
>>> you the choice.
>>
>> C++ gives you *more* choice by allowing easier migration between
>> the two without imposing performance or syntactic differences.
> 
> That isn't my experience. In my work on DMDscript in D, I changed some
> types between struct and class to try out some variations, and found it
> to be a lot easier than in C++. For one thing, I didn't have to find and
> edit all the -> and .'s.

In C++, no, you didn't, as structs and classes are no different
in their use of "->" and "." (as you're aware, they're all just
classes).

Now, if you changed from using objects by value to using them
by reference, it would be dangerous to take the D route and use
almost the same syntax for both (for example, you inherit the
ubiquitous Java bug of writing MyType foo; and finding that foo
is null rather than being a usable object).

Of course there are two things being conflated here:
(1) the difference between structs and classes, and
(2) the difference between entity/reference types and value types

In D, this "confusion" is not a confusion, as struct/class are
there to model the latter distinction.  In C++, you can take
entity objects and drop them on the stack (in perfectly sane
code), and you can take value objects and work with them on
the heap (as you can in D, I believe); it makes the two
dimensions orthogonal.

(This has an analog in access control: in C++ access control
and virtual-ness are orthogonal, in Java and D they are not.
Orthogonality has advantages, but also has some consequences
that are arguably counter-intuitive.)

> But you're right that C++ allows you more choice - you can create a
> polymorphic type with value semantics. I think D is better off without
> them, however.

Your call.  I don't agree, but that's fine!

-- James



More information about the Digitalmars-d mailing list