Bug 9992 – Default argument from member variable

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-04-25T19:54:34Z
Last change time
2024-12-13T18:06:25Z
Assigned to
No Owner
Creator
Luís Marques
Moved to GitHub: dmd#17584 →

Comments

Comment #0 by luis — 2013-04-25T19:54:34Z
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
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