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

Calling a closure stored in a struct, a bug or just a bad error message #18343

Closed
dutt opened this issue Oct 26, 2014 · 4 comments · Fixed by #18346
Closed

Calling a closure stored in a struct, a bug or just a bad error message #18343

dutt opened this issue Oct 26, 2014 · 4 comments · Fixed by #18346
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@dutt
Copy link

dutt commented Oct 26, 2014

I tried to do this:

struct S {
    x : uint,
    func : fn(uint)
}

impl S {
    fn no_func(val : uint) { println!("{}", val); }
    fn new() -> S {
        S { x : 0, func : S::no_func }
    }
}

fn main() {
    let s : S = S::new();
    s.func(7);
}

And I got the error:
error: typeSdoes not implement any method in scope named "func"

It seems to try to look for a member function and ignore the member variable. One person(alexchandel) in the rust irc channel said this was a bug and should work, another(p1start) said the error message should be improved.

@aochagavia
Copy link
Contributor

To get this working, you need to write (s.func)(7);. The problem is that Rust will search only for methods named func and not for fields. I don't know if there are plans of changing this behavior. If not, then I guess it would be nice to get a clearer error message which explains this.

@huonw huonw added the A-diagnostics Area: Messages for errors, warnings, and lints label Oct 26, 2014
@huonw
Copy link
Member

huonw commented Oct 26, 2014

I've tagged the bug so that it covers improving the error message by adding something like note: write (s.func)(...)if you meant to call the function stored in thefunc field.

@aochagavia
Copy link
Contributor

@huonw I guess we would need to check here if the type has a field with the same name as the method.

@dutt
Copy link
Author

dutt commented Oct 26, 2014

@aochagavia I got it working with the nice people in the channel but thanks anyway.

@huonw Alright, sounds good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants