Hiding class pointers -- was it a good idea?

Regan Heath regan at netmail.co.nz
Wed Aug 15 09:57:47 PDT 2007


Gregor Richards wrote:
> Bill Baxter wrote:
>> Gregor Richards wrote:
>>> #1 advantage of making them always by-value: Ridiculously 
>>> inconsistent use of by-value passing, ref passing and pointer passing.
>>>
>>> void foo(Thing a);
>>> void foo(ref Thing a);
>>> void foo(Thing *a);
>>>
>>> Wait, that's not an advantage at all, that's C++.
>>
>> I don't get your point.  Each of those things has different uses in C++.
>>
>> // if Thing is small/PoD and you don't want changes to affect caller
>> void foo(Thing a);
>>
>> // if Thing is bigger, you want to allow caller-visible changes, and 
>> you  require calling with non-null
>> void foo(ref Thing a);
>>
>> // same as above, but you want to allow null too
>> void foo(Thing *a);
>>
>>
>> The ref-can't-be-null thing may not hold for D, but it's true of C++.
>>
>> --bb
> 
> This just makes writing code more complicated. It's difficult to 
> remember whether some function wants a reference or a pointer, and you 
> have to be careful about how you treat them because they're 
> fundamentally different (see the operator overloading post). The 
> advantage is virtually nil, and it would create this huge complication. 
> That's why it's terrible in C++, and that's why it would be terrible in D.

We have the same problem in D with value types like 'int'.

Pass by reference has always bothered me.  It hides what's going on at 
the call site, eg.

int a;
foo(a);
bar(a);

which one modifies a?

Then there is the fact that you cannot pass null, meaning in some cases 
creating and passing a dummy parameter for an 'out' which you don't 
actually want.

I actually quite like pointers myself but I like D's arrays more! 
They're passed by value or by reference and you can always pass null.

If only classes could work in the same way!

Regan



More information about the Digitalmars-d mailing list