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

Type inference RecommendedWatcher #246

Open
pickfire opened this issue Feb 23, 2020 · 2 comments
Open

Type inference RecommendedWatcher #246

pickfire opened this issue Feb 23, 2020 · 2 comments
Labels
B-doc improvement/fix for documentation Z-good first issue

Comments

@pickfire
Copy link

Since there can only be one RecommendedWatcher, why do we still need to explicitly specify the type? Taken from the new docs.

use notify::{Watcher, RecommendedWatcher, RecursiveMode, Result};

fn main() -> Result<()> {
    // Automatically select the best implementation for your platform.
    let mut watcher: RecommendedWatcher = Watcher::new_immediate(|res| {
        match res {
           Ok(event) => println!("event: {:?}", event),
           Err(e) => println!("watch error: {:?}", e),
        }
    })?;

    // Add a path to be watched. All files and directories at that path and
    // below will be monitored for changes.
    watcher.watch(".", RecursiveMode::Recursive)?;

    Ok(())
}

Not sure if we can remove the explicit type for RecommendedWatcher.

@passcod
Copy link
Member

passcod commented Feb 24, 2020

You can use the short method with no types:

let mut watcher = immediate_watcher(...)?;

The full form (needs a type hint because Watcher is a trait):

let mut watcher: RecommendedWatcher = Watcher::new_immediate(...)?;

means it's easier to switch implementation:

let mut watcher: INotifyWatcher = Watcher::new_immediate(...)?;

So it might be preferable for demonstration purposes. Otherwise it's just taste.

@pickfire
Copy link
Author

Ah, I thought let mut watcher = immediate_watcher(...)?; should be more preferably for a demo to attract attention on the api with shortest code. let mut watcher: INotifyWatcher = Watcher::new_immediate(...)?; may be useful later in the first page, explaining how to switch implementation.

@JohnTitor JohnTitor added B-doc improvement/fix for documentation Z-good first issue labels Jul 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-doc improvement/fix for documentation Z-good first issue
Projects
None yet
Development

No branches or pull requests

3 participants