Skip to content

Missing type hint on closure arg blows up. #21034

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

Closed
jfager opened this issue Jan 12, 2015 · 1 comment
Closed

Missing type hint on closure arg blows up. #21034

jfager opened this issue Jan 12, 2015 · 1 comment
Labels
A-closures Area: Closures (`|…| { … }`) A-type-system Area: Type system

Comments

@jfager
Copy link
Contributor

jfager commented Jan 12, 2015

This works:

use std::collections::hash_map::HashMap;

fn main() {
    let mut cmds: HashMap<String, Box<FnMut(&str)->()>> = HashMap::new();
    cmds.insert("ping".to_string(), Box::new(|&mut: s: &str| { println!("{}", s); }));
    cmds.get_mut("ping").map(|f| f("pong"));
}

While this doesn't:

use std::collections::hash_map::HashMap;

fn main() {
    let mut cmds: HashMap<String, Box<FnMut(&str)->()>> = HashMap::new();
    cmds.insert("ping".to_string(), Box::new(|&mut: s| { println!("{}", s); }));
                                                  //^ no type hint
    match cmds.get_mut("ping").map(|f| f("pong"));
}
<anon>:6:37: 6:79 error: type mismatch: the type `closure[<anon>:6:46: 6:78]` implements the trait `core::ops::FnMut(_)`, but the trait `for<'r> core::ops::FnMut(&'r str)` is required (expected concrete lifetime, found bound lifetime parameter )
<anon>:6     cmds.insert("ping".to_string(), Box::new(|&mut: s| { println!("{}", s); }));
                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:6:37: 6:79 note: required for the cast to the object type `for<'r> core::ops::FnMut(&'r str)`
<anon>:6     cmds.insert("ping".to_string(), Box::new(|&mut: s| { println!("{}", s); }));
                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

It would be nice if the argument type could be inferred; having to hint every arg can be very verbose.

Possibly related to #20558, #20254, #19499, but this is a different error message.

@kmcallister kmcallister added A-type-system Area: Type system A-closures Area: Closures (`|…| { … }`) labels Jan 12, 2015
@jfager
Copy link
Contributor Author

jfager commented Jan 31, 2015

Works as of a recent nightly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

2 participants