Explicitly saying ref or out when invoking a function

Bill Baxter wbaxter at gmail.com
Tue Aug 11 13:24:30 PDT 2009


On Tue, Aug 11, 2009 at 12:47 PM, Michiel
Helvensteijn<m.helvensteijn.remove at gmail.com> wrote:
> Ary Borenszweig wrote:
>
>> In C# when you define a function that takes an out or ref parameter,
>> when invoking that function you must also specify ref or out. For example:
>>
>> void fun(ref uint x, double y);
>>
>> uint a = 1;
>> double b = 2;
>> fun(ref a, b);
>>
>> What do you think?
>
> I see what you mean, however:
>
> -----
>
> swap(ref a, ref b);
>
> I think that's overly verbose for a call with very descriptive function name
> to begin with.

I don't think that looks so bad.  It's not like swap is used in even 1
out of 100 lines of code generally.
And it's not an expression either, so it generally is written on a
line all by itself.

> -----
>
> The designer of the function can always request pointers, so the caller has
> to explicitly pass addresses.

But pointers can be null.  Refs cannot.  That's one big advantage of ref args.
Of course if D gets non-nullable pointers...

Anyway, I generally like the idea of requiring 'ref' on the call side.
 But I doubt Walter will go for it.  He didn't the last time it was
suggested.

--bb



More information about the Digitalmars-d mailing list