D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 9992 - Default argument from member variable
Summary: Default argument from member variable
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-04-25 19:54 UTC by Luís Marques
Modified: 2024-12-13 18:06 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Luís Marques 2013-04-25 19:54:34 UTC
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 yebblies 2013-11-21 08:24:51 UTC
Default arguments are evaluated at the call site... so I don't think this is necessarily wrong.
Comment 2 bearophile_hugs 2013-11-21 08:31:01 UTC
(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 yebblies 2013-11-21 08:43:23 UTC
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 dlangBugzillaToGithub 2024-12-13 18:06:25 UTC
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