Out parameters and initialization

Unknown W. Brackets unknown at simplemachines.org
Sun Feb 26 14:44:57 PST 2006


Yes, but this is a topic about a bug.  We are no longer really talking 
about the bug.

If you used your workaround, you would have:

1. Extra global variables somewhere in memory, which may not be desirable.

2. Uninitialized variables, from what I can tell (out parameters are 
normally initialized to their default state at the beginning of the 
function.)  This may cause unexpected/hard-to-reproduce bugs.

3. Requirements to use synchronized blocks around accesses to the 
variables, which would normally not be necessary with typical out 
parameters.  This would be needed because the function may depend on the 
global variables not changing mid-function, but the function might get 
called numerous times concurrently in multiple threads.

It's true that programming involves the above all the time, but they can 
all be avoided in this case by not using your described method.  As 
such, I consider them flaws in the proposed method.

-[Unknown]


> Unknown W. Brackets wrote:
>> Obviously that has nothing to do with what I meant.
> 
> Uh, sorry I'm tired.
> 
>>
>> Consider:
>>
>> int foo(out C x, out C y, out C z);
>>
>> It is possible I may not want to specify a y or a z.  I may however 
>> still want the return value and x.  In other parts of my code, I may 
>> want x and y, or all three.  Only in some places will I not want z or y.
>>
>> You clearly misunderstood me.  Using in has absolutely nothing to do 
>> with this.
> 
> I see that now:
> 
>>
>> Currently, a workaround would be:
>>
>> int foo(out C x)
>> {
>>    C dummy1, dummy2;
>>    return foo(x, dummy1, dummy2);
>> }
> 
> No: a workaround would be:
> 
> private int dummy1, dummy2;
> void foo(out int x = dummy1, out int y = dummy2, out int z = dummy3){...}
> 
> int foo(out C x)
> {
>    return foo(x);
> }
> 
> It complicates library code just a little but simplifies users code.
> 
>>
>> But this really is going to the side a bit of the topic.
> 
> I thought the topic was initialization of out parameters.



More information about the Digitalmars-d-bugs mailing list