michaelbull / kotlin-result

A multiplatform Result monad for modelling success or failure operations.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implement method into_ok_or_err for Result<T, T>

olback opened this issue · comments

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.

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

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.

Does this differ from merge?

commented

Nope, I just totally missed that method.

Thank you!

No worries, here's the wiki entry for it if that helps.