patterns icon indicating copy to clipboard operation
patterns copied to clipboard

Add fallible closure to idioms

Open jRimbault opened this issue 4 years ago • 6 comments

This is a short idiom I think I've seen several times.

Using a fallible closure inside an infallible function to allow the use of the ? operator.

jRimbault avatar Mar 29 '21 10:03 jRimbault

@jRimbault Do you still want to add anything or is it ready to review?

simonsan avatar Mar 29 '21 19:03 simonsan

I think it would be good to elaborate a bit more and give a better example, but yes I'm ready to review and change things if needed ?

jRimbault avatar Mar 29 '21 19:03 jRimbault

This was prompted by someone on reddit asking how to write the first two functions, to which I replied with the 3rd implementation. I think I've seen that idiom here and there in Rust code.

jRimbault avatar Mar 30 '21 13:03 jRimbault

The general question is also when you should do it and when not. I mean it's obviously pretty bad for error handling and I would consider it kind of an anti-pattern somehow in this regards, as also mentioned in the # Disadvantages. Also it's quite unclear (for me as a reader) why you would use try/? to bubble up errors, but then don't handle them. Maybe to just sketch out/hack something fastly? I think in this regards the text could elaborate a bit more, imho.

simonsan avatar Mar 30 '21 21:03 simonsan

I'll sleep on on your advice (it's 11pm in my timezone). I think I should first find some good examples (of good* and bad use cases). I've only had one legit use case. It's often came up when trying to help someone else though. IMO this should definitely not be in a function in a public API. It's really about syntax convenience when the author thinks they have a good default case.

jRimbault avatar Mar 30 '21 21:03 jRimbault

Any updates on this? 🚀

simonsan avatar Dec 23 '23 04:12 simonsan