8000 Should tap allow the closure to return an ignorable value? · Issue #5 · myrrlyn/tap · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Should tap allow the closure to return an ignorable value? #5

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

Open
hzqd opened this issue Jan 10, 2022 · 1 comment
Open

Should tap allow the closure to return an ignorable value? #5

hzqd opened this issue Jan 10, 2022 · 1 comment

Comments

@hzqd
Copy link
hzqd commented Jan 10, 2022

I code this:

use std::io::stdin;
String::new().tap_mut(|s| stdin().read_line(s).unwrap())

Getting error message:

xx |     String::new().tap_mut(|s| stdin().read_line(s).unwrap())
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `usize`

It can be solved by adding brace and semicolon to the closure, but that doesn't seem ergonomic enough.

Do you think tap should allow the closure to return an ignorable value?

@tgnottingham
Copy link

I don't think it should, because it introduces a new class of potential bugs. The current design prevents the user from falling into the trap of thinking that tap returns the value that the closure returns.

Here's a contrived example where the user might believe that this would print "HELLO HELLO", but it would only print "HELLO".

use tap::prelude::*;

fn echoed(s: &str) -> String {
    s.to_string() + " " + s
}

fn main() {
    let echo = "hello".to_ascii_uppercase().tap(|s| echoed(s));
    println!("{echo}");
}

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

No branches or pull requests

2 participants
0