-
Notifications
You must be signed in to change notification settings - Fork 226
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
Methods not found when using lambdas #6802
Comments
Seems weird but what happens when you provide a typo hint inside of the lambda? |
It works:
|
It seems what's happening is that |
@asterite is right, we don't "push down" types at all during type checking. We only infer from the bottom up. This often means lambda arguments in particular will need type annotations if you're calling methods or accessing fields in the lambda. |
I was wondering how Rust solves this, because it works in Rust. But if you have this: pub struct Foo {}
impl Foo {
fn foo(self) {}
}
fn main() {
let lambda = |foo| foo.foo();
lambda(Foo {});
} then it doesn't compile (it asks you to put a type annotation on If you have this it compiles fine: pub struct Foo {}
impl Foo {
fn foo(self) {}
}
pub fn call(_f: fn(Foo) -> ()) {}
fn main() {
call(|foo| foo.foo());
} So I can imagine Rust sees that pub struct Foo {}
impl Foo {
fn foo(self) {}
}
pub fn call<T>(_t: T, _f: fn(T) -> ()) {}
fn main() {
call(Foo {}, |foo| foo.foo());
} and even this: pub fn call<T>(_f: fn(T) -> (), _t: T) {}
fn main() {
call(|foo| foo.foo(), Foo {});
} but not this: pub fn call<T>(_f: fn(T) -> (), _g: fn() -> T) {}
fn main() {
call(|foo| foo.foo(), || Foo {});
} |
Rust uses bidirectional type checking, so it pushes types down as well |
Aim
The code below is a simplification of something I was attempting when I run into compiler errors:
This errors out with
I'm not sure what to infer from this, since the example is quite minimal. I imagine the issue would become evident when debugging.
The text was updated successfully, but these errors were encountered: