Not initialized out argument error
bearophile via Digitalmars-d
digitalmars-d at puremagic.com
Tue Jun 17 02:55:08 PDT 2014
Walter Bright:
>and D's 'out' has nothing to do with strictness.<
Strictness is how a language handles every own feature :-)
>Frankly, I think D's parameter classes and array slices are far
>simpler and more effective than SAL2.<
I have not used SAL so I can't tell. It has nonnull compile-time
tests, and tests of copy or write of the _whole_ span of an
array, that look nice and are missing in D.
>> How much often do I need to write a function like foo?
>> I think it's uncommon.
>I suspect that is a guess.
I've written a ton of D1/D2 code, and functions like foo are very
uncommon in my code. I can do a statistic on my code.
>I see it as annoying and nothing to do with 'strictness'.<
"out" arguments are return values of a function, so the following
are logically the same:
int foo() { return 0; }
void foo(out int x) { x = 0; }
Here strictness means respecting that basic semantic meaning of
the function. So accepting a function like this because we assume
x gets initialized at entry point is less strict:
void foo(out int x) {}
And it's not annoying for me because I almost never write
functions with out arguments that I don't want to initialize
inside the function. (But perhaps other people write code
differently, I'd like to know what they do).
>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.<
Yes, this change in out semantics will break of that more general
rule. But I think when you use an out argument you are asking for
a meaningful return value. Not initializing a variable because
you need a zero is common, but you usually don't call a function
with an out argument because you want a init value. So I think
the two cases are sufficiently different.
>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 :-)<
Right. But how often do you want to initialize an out argument to
its "init" in a function?
--------------------
I think Tobias Pankrath is saying that accepting code like this:
void foo(out int x) {}
Is just like the compiler accepting code like this:
int foo() {}
and rewriting it like this:
int foo() { return typeof(return).init; }
Currently the compiler doesn't perform that rewrite, and instead
it gives a missing return value error.
D even gives errors if you do this:
int foo(in bool b) {
if (b)
return 1;
}
So it asks for all cases to return something or assert(0).
Bye,
bearophile
More information about the Digitalmars-d
mailing list