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).
Default arguments are evaluated at the call site... so I don't think this is necessarily wrong.
(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.
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.
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17584 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB