Out parameters and initialization

Derek Parnell derek at psych.ward
Sun Feb 26 19:47:30 PST 2006


On Sun, 19 Feb 2006 17:53:45 -0800, Unknown W. Brackets wrote:

> The spec says:
> 
> out parameters are set to the default initializer for the type of it.
> 
> Which makes perfect sense, and works just fine.  The trick comes in when 
> I try some parameter initialization. 

I'm writing this without reading the entire thread, so please excuse me if
I'm out of line.

I also preface these remarks by noting that the documentation is
exceedingly light on the whole 'default parameter' specification.

Functions:
  "A function parameter's default value is not inherited ..."

 Change log for v0.92:
  "Added default arguments to function parameters. Semantics are like C++."

Anyhow, it seems that you are thinking in terms of "initialize" rather than
"default". The difference is that "default" (which is what I believe D is
implementing) is invoked by the compiler when a omit a parameter on the
call, and "initialize" is when the compiler applies a value to a parameter
which you have not omitted, and I don't think the D has implemented this
concept.

 foo(in int a) // foo gets (a copy) the value of 'a'.
 foo(in int a = 5) // foo gets 'a' if you pass it, 
                   // otherwise it gets the literal 5

 foo(out int a) // foo gets (a reference to) the value of 'a' and
                // sets the value of 'a' to the default initializer for
                // for 'int'.
 foo(out int a = 5) // foo gets a reference to 'a' if you pass it etc..., 
                   // otherwise it gets a reference to the literal 5
                   // and sets the value of 5 to 0
                   // (which doesn't make sense to me)


 foo(inout int a) // foo gets (a reference to) the value of 'a'.
 foo(inout int a = 5) // foo gets a reference to 'a' if you pass it, 
                   // otherwise it gets a reference to the literal 5
                   // (which doesn't make sense to me)

So I think that defining default parameters for 'out' and 'inout' arguments
should be a compile-time error, because in these cases the called program
will be receiving the address of a literal.

Can Walter clarify the intent of the default parameter syntax when used in
conjunction with 'out' and 'inout'?

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
27/02/2006 1:38:08 PM



More information about the Digitalmars-d-bugs mailing list