In DMD64 v2.062, if you define a method with a default argument from a member variable, DMD only complains if the method is actually called:
class Foo
{
int a = 0;
void bar(int x = a)
{
}
}
void main()
{
Foo f = new Foo();
//f.bar(); -> does not complain with this commented out
}
With the method call it produces the error "need 'this' to access member a", which seems to be the defined behavior (but why not allow the default argument to be variable? you can just read 'a' and push it on the stack).
Comment #1 by yebblies — 2013-11-21T08:24:51Z
Default arguments are evaluated at the call site... so I don't think this is necessarily wrong.
Comment #2 by bearophile_hugs — 2013-11-21T08:31:01Z
(In reply to comment #1)
> Default arguments are evaluated at the call site... so I don't think this is
> necessarily wrong.
I think the compiler should give errors for wrong code, even if the function is not yet called.
Comment #3 by yebblies — 2013-11-21T08:43:23Z
Hmm, this doesn't work either.
class Foo
{
int a = 0;
void bar(int x = a)
{
}
void baz()
{
bar();
}
}
void main()
{
Foo f = new Foo();
//f.bar(); -> does not complain with this commented out
}
They probably should be banned.
Comment #4 by robert.schadek — 2024-12-13T18:06:25Z