Ref parameter: intended behavior or bug?

Regan Heath regan at netmail.co.nz
Wed Aug 22 05:07:04 PDT 2007


Jarrett Billingsley wrote:
> "Regan Heath" <regan at netmail.co.nz> wrote in message 
> news:fah2ic$2e20$1 at digitalmars.com...
>> However, the liability (IMO):
>>
>>   Which of these functions modifies 'foo'?
>>
>>   Foo foo;
>>   bar(foo);
>>   baz(foo);
>>
>> You can't tell without inspecting the function signatures. Whereas with 
>> pointers:
>>
>>   Foo foo;
>>   bar(&foo);
>>   baz(foo);
>>
>> It's quite clear, unless of course baz is passing by reference ;)
> 
> If D required 'ref' and 'out' at call site like C#, this wouldn't be an 
> issue. 

True.  I think I'd prefer just to use pointers.

Then, you're writing & at call site and *param when you're modifying the 
parameter itself, both of which are explicit and easy to see.

D automatically dereferences member operations for pointers to structs 
so the syntax is already fine there.

The one problem you have are operator overloads, eg.

struct Foo { ... }

Foo foo(Foo* a, Foo* b)
{
	return a * b;
}

has to be written:

Foo foo(Foo* a, Foo* b)
{
	return *a * *b;
}

which is a bit ick.

Regan


More information about the Digitalmars-d-learn mailing list