[DIP idea] out variables
Q. Schroll
qs.il.paperinik at gmail.com
Sun Jan 31 01:39:27 UTC 2021
On Thursday, 28 January 2021 at 09:17:47 UTC, Dukc wrote:
> int f(out int, int);
>
> int func()
> { out int x;
> if(someCond) x.f(0);
> else if(someOtherCond) x.f(1);
> return x;
> }
>
> What should the compiler do? It cannot know whether it's
> possible x can be returned uninitialized. It can issue an error
> just in case, and we hate to refactor code due to false alarms
> like that.
Exactly this. Something similar is true for an immutable
constructor:
struct S
{
int a;
this(int x) immutable
{
if (x < 0) { a = 1; }
else if (x > 0) { a = 2; }
if (x == 0) a = 3; // error
}
}
This problem is inherent.
More information about the Digitalmars-d
mailing list