One document about Go

bearophile bearophileHUGS at lycos.com
Tue Jun 1 15:00:37 PDT 2010


Walter Bright:

> bearophile wrote:
> > D language
> > isn't even able to tell if an "out" argument has being used before it is
> > assigned inside the function (as C# compiler is able to, because in C# using
> > uninitialized variables is an error) This is awful and surely worse than
> > multiple return values.
> 
> That's a misunderstanding of how "out" parameters work in D. All out parameters 
> are default initialized upon entry to the function. Therefore, the user cannot 
> access an uninitialized out parameter.

I know how out parameters work in D, I've probably written more D1 code than you :-) But a function argument that "comes out" is quite unnatural, it's like washing yourself with a towel and then drying up yourself with a water shower :-)
It's better to use multiple return values. Currently in D2 I prefer to return a std.typecons.Tuple instead of using out arguments.

Out parameters are initialized at function entry, so in theory all is good and there are no bugs, but this is a *workaround*, a language kludge, a hack, something dirty that is done because of language limitation, or to keep the language compatible with an ancient C design, or because the design is old. Because (as you can see in my example) you can use/read/write an out argument before calling the function (if you write it before function call, such value gets overwritten), or just after the function entry, and such usages while "safe" are not nice. "out arguments" are results, and a result is not meant to be used or read before the call of the function or before you have put something into it. I understand you can't/don't want to follow Java and C# route of not default-initializing variables (even if I prefer a how C# and Java act here), but in the case of out arguments this D design decision leads to bad code. Allowing multiple return values in D3 can improve this.

Bye,
bearophile


More information about the Digitalmars-d mailing list