-
Notifications
You must be signed in to change notification settings - Fork 55
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
Proposal: add getErr() function to UnwrapException #9
Comments
I'm not a fan of throwing exceptions just to catch them to get some information out. I think a better approach is to instead have something along these lines: fun exampleA(): Result<Int, String> = TODO()
fun exampleB(): Result<Int, String> = TODO()
data class Example(
val a: Int,
val b: Int
)
fun runExample(): Result<Example, String> {
val a = exampleA().getOrElse { return Err(it) }
val b = exampleB().getOrElse { return Err(it) }
return Ok(Example(a, b))
} The only downside is you end up recreating another |
+1 BTW, thanks for this work. I found it after reading Scott Wlaschin's "Domain Modeling Made Functional" and have yet to run into something I cannot do. In fact, I was inspired to write a short blog post here: http://markbucciarelli.com/posts/2020-01-04_kotlin_functional_gold_mine.html) |
just in case someone stumbles onto this issue, the functionality described is best captured by a monad comprehension. |
When creating a domain entity with more than five fields (so zip is not available), the best pattern I've found goes like this:
What I'd like to be able to do is return the original DomainMessage from the catch block:
Does this seem reasonable to you?
---- Edit: in proposed catch block, change Exception to UnwrapException
The text was updated successfully, but these errors were encountered: