-
-
Notifications
You must be signed in to change notification settings - Fork 134
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
Consider an explicit Result monad #42
Comments
Hi @sporto, thanks for the ideas here! However, what you're proposing isn't technically a monad anymore, so I'm not sure if it fits in this library, which is designed to offer monad functionality made as nice as possible for Ruby. It's true that Ruby is not Haskell, but I reckon working with monads in Ruby can still feels pretty natural once you've learnt the small API they offer. And in doing this with You don't always need to check for result = format_time("invalid input")
time_str = result.fmap { |time|
"The time is #{time}"
}.or_fmap { |err|
"Error determining time: #{err}
}.value And if you think that looks too bulky, it's the perfect thing to hide behind a well-named method ;) If you want exceptions to be raised, perhaps a one-liner like this (accessing time_str = result.right or raise "error calculating time" Of course, I'm sure you've considered all of these things :) So even if your proposed "explicit result" object isn't something we incorporate in dry-monads, it'd be pretty easy for you to write as a wrapper: class ExplicitResult < SimpleDelegator
FailureError = Class.new(StandardError)
def value
if success?
super
else
raise FailureError
end
end
end Something like this you could pop into your app or even a gem on its own? @flash-gordon any thoughts to add on this? |
@timriley I thought about this, like, yesterday. The reason we have user = create_user.(data).value!
# returns a user object or raises an error with the provided message
user = create_user.(data).value!("No user")
# can use blocks for messages
user = create_user.(data).value! { "No user" } Left values can be unwrapped with failure.flip.value! @sporto you should use |
@flash-gordon I really like the idea of And on the plus side, I think this would give @sporto the behavior he wants, too? |
@timriley as a side note, there is a simple reason why this semantics is aligned with other statically typed languages. |
Thanks |
The Either monads doubles as Result in dry-rb. However I found the way
.value
works to be unexpected.Consider this:
This snippet would produce a wrong output e.g. "The time is Wrong Input".
Optimally I would do
.success?
before doing this. But ruby is not haskell and pattern matching is not required. So is easy to be lazy and code the happy path.So maybe consider adding a
Result
monad that:result.value
will raise error.error
e.g.
result.error
== "Wrong Input"With something like this my above snippet will raise an error instead of giving me a wrong output, which I think is preferable.
The text was updated successfully, but these errors were encountered: