-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
Add UnwrapPretty
trait
#345
Comments
Another possible idea that I'm less sure about is pub trait OkPretty {
type Output;
fn ok_pretty(self) -> Option<Self::Output>;
}
impl<T, E> OkPretty for Result<T, E>
where
E: Diagnostic + Sync + Send + 'static
{
type Output = T;
fn ok_pretty(self) -> Option<Self::Output> {
match self {
Ok(output) => Some(output),
Err(diagnostic) => {
println!("{:?}", Report::new(diagnostic));
None
},
}
}
} If I could pick one, it'd definitely be |
I kinda like this idea, but I'm also debating whether it's good to include in miette. Philosophically speaking, I think it's important to avoid unwraps as much as possible, and miette is designed with the intention of making it easier to avoid them. But I can see how sometimes you just want to unwrap, such as tests! and how That said: tests are not typically a place where I think fancy-looking diagnostics are really necessary. Fancy diagnostics are more intended for end-users of a tool. I also definitely wouldn't want |
Fair, As someone writing a compiler right now, being able to unwrap in my tests and actually see where the error happened is incredibly helpful. |
Yes, but the cost of just adding this as your own utility, since you find it useful to you, is very very low. It might be best to keep it that way. |
Fair I did in fact implement this for my project. I just meant that there are real use cases for this. Whether there are enough to justify inclusion 🤷♀️ https://github.com/esoterra/claw-lang/blob/main/crates/common/src/diagnostic.rs |
Add a trait like the following that people can choose to import that lets them call
unwrap_pretty()
on results whereE
is aDiagnostic
which is quite useful in things like unit tests.The text was updated successfully, but these errors were encountered: