Out parameters and initialization
Ivan Senji
ivan.senji_REMOVE_ at _THIS__gmail.com
Sun Feb 26 23:55:57 PST 2006
Derek Parnell wrote:
> 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.
Who in his right mind would read the entire thread. :)
>
> 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.
>
Correct.
> 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)
I must agree this doesn't make sense.
But this does:
foo(out int a = globalA)
//if you pass something a reference to it is passed
//if you don't a reference toa global int 'globalA' is passed
This (to me) makes sense, is usefull, and is implemented.
>
>
> 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.
I think constant arguments for default out/inout arguments shuld be an
error, non constant don't need to be.
>
> Can Walter clarify the intent of the default parameter syntax when used in
> conjunction with 'out' and 'inout'?
>
This would be a good thing.
More information about the Digitalmars-d-bugs
mailing list