Skip to content
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

Question: is runCatching the equivalent to F#'s result "computational expression"? #11

Closed
mbucc opened this issue Feb 28, 2020 · 4 comments

Comments

@mbucc
Copy link

mbucc commented Feb 28, 2020

Example computational expression from Scott Wlaschin's book:

https://github.com/swlaschin/DomainModelingMadeFunctional/blob/master/src/OrderTakingEvolved/PlaceOrder.Dto.fs#L71-L81

The alternative (adding more versions of zip with more and more arguments) smells wrong. I noticed in your example service code you use runCatching in a very similar way to the code linked above.

@michaelbull
Copy link
Owner

michaelbull commented Feb 28, 2020

I'm not familiar with F# so I don't really understand what's going on in the code linked

runCatching simply runs a bit of code, catching any exceptions and wrapping them in an Err to produce a Result<T, Exception>

@mbucc
Copy link
Author

mbucc commented Feb 28, 2020

I'm back to my previous question on using unwrap. :)

I'm not an F# user either, but if I think I understand the explanation SW gives in his book, which is:

// A result computational expression:
result {
   // checkAddress returns a Result<Address, Msg>
   // but x1 is of type Address.
   let! x1 = checkAddress unvalidatedAddress
   let! x2 = checkName unvalidatedName
   return ValidatedAddress(x1, x2)
}   // Type of this full expression is Result<ValidatedAddress, Msg>

The closest match I can think of with kotlin-result is unwrap() on each line, all surrounded by runCatching. I know in a previous issue, you suggested not using unwrap() in this way, but I can't think of another way (other than continuing to expand zip).

@michaelbull
Copy link
Owner

Maybe this comment helps?

@mbucc mbucc closed this as completed Mar 2, 2020
@Munzey
Copy link
Contributor

Munzey commented May 9, 2020

@mbucc just read this and your previous issue. I believe the exact thing you are looking for is called a monad comprehension.
I have opened a pr to add the functionality you are looking for:
#13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants