Not initialized out argument error

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 17 02:24:43 PDT 2014


On 6/17/2014 1:55 AM, 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.<
>
> The documentation of Microsoft SAL2, several discussions, and some other things
> I've read tell me that if you want your compiler to catch some of your bugs
> statically (and optimize well) it needs to be strict.

Microsoft SAL2 is not about D, and D's 'out' has nothing to do with strictness. 
Frankly, I think D's parameter classes and array slices are far simpler and more 
effective than SAL2.


> How much often do I need to write a function like foo?
> I think it's uncommon.

I suspect that is a guess.


> If this little breaking change is introduced in D, I am then forced to write foo
> like this:
>
> void foo2(out int x) { x = 0; }
>
> Is this a price small enough to pay for that increase in strictness?

I see it as annoying and nothing to do with 'strictness'. D default initializes 
all variables that don't have an explicit initializer. This is normal for D, and 
is a nice convenience. There's no reason that 'out' should behave differently.

It's also a correctness feature - the default initializer is NOT always 0, and 
if you're writing generic code you'd be making a mistake to use 0. You'd have to 
initialize it to 'typeof(x).init', which is a good way of punishing users :-)



More information about the Digitalmars-d mailing list