@scottmcm I have some answer, maybe not very satisfying, but I didn't want to leave your question unanswered.
A question: what would be the Result
equivalent(s) of this? Usually they parallel each other fairly closely in the methods on offer (as in many ways Option<T> <=> Result<T, ()>
) so whether for_ok/for_err
would be good/bad things might be indirect evidence for/against this method.
I'm not entirely sure if something like for_ok
would have a good meaning or use.
I agree with you that similarity between these types should be considered, so I guess I should bring some arguments why Option::for_some
makes sense, while Result::for_ok
probably not.
Although there are similarities, there are differences as well (why to have two types otherwise, right?).
This might be one of the cases where the differences show up.
The primary meaning of Result
is representing success or failure.
Uses of Result
are focused on function results and processing them in a safe way.
Ignoring Result
smells, the cases where neither Ok
nor Err
need any processing are probably very rare.
Err
cases can be thrown away easily with ?
operator, focusing the code flow on the happy path and the Ok
value.
How the hypothetic Result::for_ok
could be helpful and how it would fit in?
I would expect it to help consuming the Ok
value and I would expect both these options to work as noted:
// Handle Err as usual
fallible_operation().for_ok(|ok| println!("We successfully generated {}.", ok))?;
// Ignore Err completely
fallible_operation().for_ok(|ok| println!("We successfully generated {}.", ok));
In both cases the value of the statement would be ()
. Compare it to the code without for_ok
:
// Handling Err
let ok = fallible_operation()?;
println!("We successfully generated {}.", ok);
// Alternatively
println!("We successfully generated {}.", fallible_operation()?);
// Ignoring Err
if let Ok(ok) = fallible_operation() {
println!("We successfully generated {}.", ok);
}
When handling Err
, for_ok
does not help much in my opinion.
However, with for_ok
it is easy to ignore Err
, even by an accident.
It seems dangerous to me and I think this looks like KO for for_ok
.
Regarding for_err
, I think it is a lost case from the beginning and not just because for_ok
seems to lead in a bad direction.
As I noted before, I don't see something like Option::for_none
as a good idea.
The less reasons there I would expect in favor of Result::for_err
.
Why the failure with Result::for_ok
might not be relevant for rejecting Option::for_some
?
Option
is more generic and it does not (or should not) indicate success/failure.
Therefore not acting on None
(or Option
in general) does not smell like ignoring Err
(or Result
in general).
So, a short answer might be: because for_some
brings no danger.
By the way, for_some
could indirectly extend the possibilities of Result
:
// Ignore Err completely, but in a safer way
fallible_operation()
.ok()
.for_some(|ok| println!("We successfully generated {}.", ok));