Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default argument from member variable #17584

Open
dlangBugzillaToGithub opened this issue Apr 25, 2013 · 3 comments
Open

Default argument from member variable #17584

dlangBugzillaToGithub opened this issue Apr 25, 2013 · 3 comments

Comments

@dlangBugzillaToGithub
Copy link

Luís Marques (@luismarques) reported this on 2013-04-25T19:54:34Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=9992

CC List

  • yebblies

Description

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).
@dlangBugzillaToGithub
Copy link
Author

yebblies commented on 2013-11-21T08:24:51Z

Default arguments are evaluated at the call site... so I don't think this is necessarily wrong.

@dlangBugzillaToGithub
Copy link
Author

bearophile_hugs commented on 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.

@dlangBugzillaToGithub
Copy link
Author

yebblies commented on 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant