[Bug 191] New: Cannot refer to member variable in default value for method parameter

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Tue Jun 13 06:07:57 PDT 2006


BCS wrote:
> Deewiant wrote:
>> Jarrett Billingsley wrote:
>>
>>> Is this even in the spec?  I had no idea you were supposed to be 
>>> allowed to use members as default parameters.
>>
>>
>> All I can find about default parameters in the spec is at the 
>> "Functions" page,
>> where they pop out of the blue in the phrase "A function parameter's 
>> default
>> value is not inherited", which is all that's said about them. The 
>> changelog for
>> DMD 0.92 also says "Added default arguments to function parameters. 
>> Semantics
>> are like C++." but that's it.
> [...]
> 
> Is this correct behavior? If it is it needs to be documeted better.
> 
> <code>
> import std.stdio;
> 
> class C        { int foo(int x = 1){return x;} }
> class D:C    { int foo(int x = 2){return x;} }
> 
> 
> void main()
> {
>     C c = new D;
>     D d = cast(D)c;
> 
>     writef(c.foo,\n);    // prints 1 (?)
>     writef(d.foo,\n);    // prints 2
> }
> </code>

Hum, quite a catch you got there! Not only should it be better 
documented, but maybe it shouldn't work this way.
Perhaps the default values should be set not at the call site, but by 
the function itself (thus depending on it's run-time type, and not it's 
compile-time type), or maybe it could be as Oskar's idea ("if D 
inherited default values and forbade them to be redefined in derived 
classes")

-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d-bugs mailing list