-
Notifications
You must be signed in to change notification settings - Fork 9
Improving ergonomics with Result? #12
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
Comments
Thanks @leeola! I see the motivation. It does complicate the semantics of when the In your use case, and any other plausible use cases you're thinking of, would the |
I would imagine That's why in my straw man example I had assumed .. to counter my own statement though, in the final version of the code that motivated this post I ended up returning If this feature is being added for the purpose of ergonomics, perhaps keeping it Difficult choice :) |
Can you give some examples how you currently need to write it with the existing replace_with_or_return*-functions and how you would like to write it with a new try_replace_with-function? Then we can easier figure out the best ergonomical solution. |
Not sure what examples I can give, beyond just wanting
There may be fine details to debate, but I think |
@leeola in your example you don't even use foo so you don't need the replace_with function. But the real problem is that you can't use the ?-operator if the F-closure should return Result<T, (T, E)> since (T, E> doesn't implement From. A solution might be your original proposal that the F-closure should return Result<T, E> and that the default closure should be called when an error is returned. But as @alecmocatta mentioned, it complicates the semantics when the default closure is called. That might not be a problem, but we need to make sure that that is what we really need. The problem is that it might not be obvious for the caller, or at least not a reader of the callers code, that the value in the mutable reference has changed to some unrelated default value when an error occurred.
This shows that the wanted behavior of the function is unclear and hence it shouldn't maybe be implemented. After all, it isn't that difficult to write a function anyway. Either as @leeola did by preserving the already allocated value, or with a fallback function to generate a default value in case of an error. |
Yea it wasn't a working example hah. I was literally just talking about having
Agreed
I disagree with the wanted behavior being unclear. My original intention (when this card was posted) was that the return signature made This card isn't describing an unsolvable problem. You don't need I'm okay with either, I'll let ya'll hash it out. I just figured I should make the request against the library, or possibly write my own func to handle it. Appreciate the library! Thanks :) |
When try blocks gets stabilized, this will be easier and more clear. For example:
|
Thanks both for putting in the time to explore the solution space! Try blocks do seem to be approaching stabilisation – a big outstanding design decision having been recently resolved. If we operate under the assumption stabilisation is forthcoming, how do you feel @leeola about @tage64's suggested solution above? |
While not being terribly familiar with try blocks, it looks to solve the ergonomics problem nicely. Minimal syntax addition to use |
Great, going to close this as resolved but feel free to update if |
The
replace_with_and_return
F
signature ofFnOnce(T) -> (U, T)
is rather awkward to use withResult
'ing code.What are your thoughts on a function that behaves similarly to
replace_with_and_return
, but tweaked to supportResult
and/orOption
? A non-optimal path, but not quite a panic either. Something like:I imagine the panic/abort behavior would remain the same. Using the default where applicable.
Though, I could see someone requesting a
panic_default: D
anderr_default: D
, but those don't match my use case as much.Thoughts?
The text was updated successfully, but these errors were encountered: