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

Ergonomic Suggestion for Option Arguments #25

Open
jsouto18 opened this issue Feb 22, 2020 · 2 comments
Open

Ergonomic Suggestion for Option Arguments #25

jsouto18 opened this issue Feb 22, 2020 · 2 comments
Labels
question Further information is requested

Comments

@jsouto18
Copy link

Hey,
While using this library I felt like it could benefit ergonomically from changing the signature of two functions:

pub fn set_fg(&mut self, color: Option<Color>) -> &mut ColorSpec {
    // ...
}
pub fn set_bg(&mut self, color: Option<Color>) -> &mut ColorSpec {
    // ...
}

Changing them to:

pub fn set_fg(&mut self, color: impl Into<Option<Color>>) -> &mut ColorSpec {
   let color = color.into()
   // ...
}
pub fn set_bg(&mut self, color: impl Into<Option<Color>>) -> &mut ColorSpec {
   let color = color.into()
   // ...
}

This would allow for developers, using this library, to configure a ColorSpec struct like this:

ColorSpec::new().set_fg(Color::Red);

Rather than:

ColorSpec::new().set_fg(Some(Color::Red));

This would also be backwards compatible, meaning that any case of .set_fg(Some(Color::Red)) would still be valid.

I would be happy to make a PR if you decide that this would be beneficial for the library.

Thank you!

@BurntSushi
Copy link
Owner

I understand why you're making this suggestion, but I've never been a huge fan of the impl Into<Option<...>> trick. I generally don't like its implicitness and find that it makes the call sites more difficult to read.

I'm not totally opposed to it though. I'll noodle on it. If you want to put up a PR, that would be great!

@BurntSushi BurntSushi added the question Further information is requested label Feb 22, 2020
@Property404
Copy link

Consider also, you could have a Color::None or Color::Default or whatever variant. That way, you wouldn't have to deal with Options anymore. You get the ergonomic benefit of doing ColorSpec::new().set_fg(Color::Red), while maintaining a simple (actually, simpler) function signature

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

No branches or pull requests

3 participants