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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jun 11 05:06:33 PDT 2006


http://d.puremagic.com/bugzilla/show_bug.cgi?id=191

           Summary: Cannot refer to member variable in default value for
                    method parameter
           Product: D
           Version: 0.160
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P3
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: deewiant at gmail.com


--
class c {
        int x = 5;
        int foo(int a = x) { return a; }
}

void main() {
        assert ((new c).foo() == 5);
}
--

The above code does not compile, producing the error "need 'this' to access
member x".

Obliging by changing "int a = x" to "int a = this.x" changes the error to
"'this' is only allowed in non-static member functions" followed by "this for x
needs to be type c not type int".

It can be worked around by removing the default value and manually overriding
foo with "int foo() { return foo(x); }", but this is a needless annoyance and
precisely what default values are there for.

It might be worth noting that this is a longstanding bug which bit me for the
first time with DMD 0.128:
http://www.digitalmars.com/d/archives/digitalmars/D/bugs/4532.html


-- 




More information about the Digitalmars-d-bugs mailing list