Bug 191 – Cannot refer to member variable in default value for method parameter

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-06-11T07:06:00Z
Last change time
2014-02-15T13:19:03Z
Keywords
rejects-valid
Assigned to
bugzilla
Creator
matti.niemenmaa+dbugzilla

Comments

Comment #0 by matti.niemenmaa+dbugzilla — 2006-06-11T07:06:33Z
-- 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
Comment #1 by bugzilla — 2008-06-23T16:43:10Z
This isn't a bug, it is the way it is designed to work. The example will work, however, if you add 'const' to the declaration of x.