Implement method into_ok_or_err for Result<T, T>
olback opened this issue · comments
Edwin commented
I think it'd be nice to be able to write something like
val a = res.map { it.joinToString("\n") }.into_ok_or_err()
instead of
val a = when (res) {
is Ok -> res.unwrap().joinToString("\n") // -> String
is Err -> res.unwrapError() // -> String
}
This feature already exists in Rust Nightly.
Michael Bull commented
Can you describe the receiver type and the return type more precisely? Struggling to understand what this is meant to take and produce with the implicit type in your examples
Edwin commented
Yes, sorry.
val res: Result<List<String>, String> = fetchList()
val output: String = res.map { it.joinToString("\n") }.into_ok_or_err()
Another example:
val res: Result<String, String> = fetchString()
val output: String = res.into_ok_or_err()
Essentially just takes the ok value if ok and err value if not ok.
Michael Bull commented
Does this differ from merge?
Edwin commented
Nope, I just totally missed that method.
Thank you!
Michael Bull commented
No worries, here's the wiki entry for it if that helps.