Out parameters and initialization
Regan Heath
regan at netwin.co.nz
Sat Feb 25 17:56:04 PST 2006
On Sat, 25 Feb 2006 16:21:23 -0800, Unknown W. Brackets
<unknown at simplemachines.org> wrote:
> That does not make it logical or consistent. It makes it confusing and
> inconsistent. Utility is not at all my concern.
Ok, so lets look at the consistency of this.
> In other words, you're essentially saying that this makes sense:
>
> var2 = 2;
> var1 = var2;
> var1 = 1;
> assert(var2 == 1);
No, he's saying that this makes sense:
void foo(inout int var1) { var1 = 1; }
int var2 = 2;
foo(var2);
assert(var2 == 1);
Remember, 'inout' aliases the variable you pass, the parameter 'var1' is
another name for the variable 'var2'.
So, when you write:
void foo(inout int var1 = 7)
what are you saying?
Lets start with what a default parameters means, in this case:
void foo(int var1 = 7) {}
says, if I do not get passed a variable, pass 7, in other words:
foo();
is called as:
foo(7);
Agreed?
Therefore:
void foo(inout int var1 = 7)
says, if I do not get passed a variable, pass 7, meaning:
foo();
is called as:
foo(7);
Agreed?
Now, as well all know you cannot pass '7' as inout because you cannot
alias '7'.
What I am trying to show here is that default parameters are behaving both
logically and consistently, they always mean: "If I do not get passed a
variable, pass X" where X is the default parameter value supplied.
Further, 'inout' is behaving both logically and consistently, in all cases
it aliases the parameter which is passed.
The behaviour of 'in'+'default parameter' is not the same as
'inout'+'default parameter' but why should it be? IMO one is an apple and
the other an orange.
Regan
More information about the Digitalmars-d-bugs
mailing list