Not initialized out argument error

monarch_dodra via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 17 02:03:29 PDT 2014


On Tuesday, 17 June 2014 at 08:55:52 UTC, bearophile wrote:
> Walter Bright:
>
>>But it is not uninitialized. All out parameters are default 
>>initialized to their .init value.<
>>I don't agree with this opinion, as they *are* initialized.<
>
> [...]
>
> void foo(out int x) {}
> void bar(out int x) { x = 10; }
>
> [...]
>
> Bye,
> bearophile

What if x is an "optional" out parameter: EG: something you only 
set if other conditions are met? Do you want to make sure that x 
is at least assigned to once, or rather make it an error to have 
a control path that *doesn't* assign anything to it.

Either way:
1. Making it an error to have a control path that doesn't assign 
to x would be counter productive, as the result would probably 
end up being code that looks like:
foo(out int x) {
    x = 0;
    ...
}
which would defeat the entire point of out.
2. Checking the variable is at least used would be kind of the 
same as checking for unused arguments. I think that'd be fine, 
provided you could over-ride the warning by not naming your 
variable:
foo(out int x){} //Error, x is never assigned to (or used)
foo(out int){}   //OK!


More information about the Digitalmars-d mailing list