dry-monads
                                
                                 dry-monads copied to clipboard
                                
                                    dry-monads copied to clipboard
                            
                            
                            
                        Add Result#value_or_raise!
While I like the idea (we're considering using monads in our project and need the same feature), the name of the method is quite confusing to me. Having failure! as a conclusion of the method call isn't what you usually expect. Maybe something like value_or_fail! or value_or_raise! could work here.
I was not convinced with failure!. I sent it before the discussions in Zulip. I like value_or_raise!, would that one work ?
@hwo411 Updated!
@hwo411 +1 for value_or_raise! name
Any interest on getting this one merged ?
Bump!
@bilby91 sorry for this delay. I don't think it's a good idea to have it as it is because it assumes there's an exception in Failure. It's not always the case. It's more suitable for the Try monad, wdyt? Apart from it, I wouldn't mind having it as an extension, though.
@flash-gordon You make a great point around failures potentially being something different than an error. Can you provide some extra insight on your thought about adding this to the Try monad please ?
Thanks!
@bilby91 two steps
- You just add value_or_raisetoTry'sValueandErrorinstead ofResult.
- a. In your code, you use Valueinstead ofSuccessandErrorinstead ofFailure. And alsoinclude Dry::Monads[:try, :do]instead ofinclude Dry::Monads[:result, :do]. b. Alternatively, you addSuccess#to_try/Failure#to_trysimilarly to how#to_success/#to_failureforTry->Resultconversion. Then in the place where you want to call.value_or_raiseyou doresult.to_try.value_or_raise.
Feel free to ask, I can do it myself.
P.S. Note value_or_raise shouldn't have ! at the end because according to ruby guidelines it should only present when there's an alternative, "safer" version of the method: fetch! vs fetch etc.
My team has been looking for similar behavior, so I wanted to check in on the status here.
One small additional piece that we've implemented in our current workaround is to copy the trace over from the Failure to help provide additional context:
def value_or_raise
  error = StandardError.new(failure)
  error.set_backtrace(trace)
  raise error
end
Would be great to see this in the project (happy to help / open an alt PR if preferable). Thanks for the fantastic project!
This would be a great addition... this UnwrapError makes little sense practically.
At least we should have the option to reraise the original error or turn anything not an error into to string and raise it...