[Bug 191] New: Cannot refer to member variable in default value for method parameter
BCS
BCS at pathlink.com
Tue Jun 13 09:48:10 PDT 2006
Bruno Medeiros wrote:
> BCS wrote:
>> <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")
>
hee hee, their is one use for this
// never call with i set explicitly
class C { abstract void doSomthing(char[] msg, int i=0); }
class D:C { abstract void doSomthing(char[] msg, int i=1); }
class E:D { abstract void doSomthing(char[] msg, int i=2); }
class F:E { void doSomthing(char[] msg, int i=3)
{
writef(`doSomthing("%s") as class %c`\n,msg, 'C'+i);
}
}
Now we known the type of the reference used to call doSomthing. (I pray
I never have reason to use this.)
More information about the Digitalmars-d-bugs
mailing list