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

Add fallible closure to idioms #250

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jRimbault
Copy link

@jRimbault jRimbault commented Mar 29, 2021

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.

tone was a bit condescending

Co-authored-by: simonsan <[email protected]>
@simonsan simonsan added C-addition Category: Adding new content, something that didn't exist in the repository before A-idiom Area: Idioms labels Mar 29, 2021
@simonsan
Copy link
Collaborator

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

@jRimbault
Copy link
Author

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
Copy link
Author

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 jRimbault marked this pull request as ready for review March 30, 2021 14:10
@simonsan simonsan changed the title [WIP] add fallible closure Add fallible closure to idioms Mar 30, 2021
@@ -19,6 +19,7 @@
- [Privacy For Extensibility](./idioms/priv-extend.md)
- [Easy doc initialization](./idioms/rustdoc-init.md)
- [Temporary mutability](./idioms/temporary-mutability.md)
- [Fallible closure](./idioms/fallible-closure.md)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might find a better headline for the summary to let this idiom be found more easily.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, this often comes up when people want to avoid "arrow code" with let guards or "match hell", maybe something around that ?

@simonsan
Copy link
Collaborator

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.


## Motivation

You may sometimes want to use the [`?`] operator inside an infallible function,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sometimes, here it would be good to give examples when that might be the case and when you would find this idiom in the wild.

## Motivation

You may sometimes want to use the [`?`] operator inside an infallible function,
in those cases a closure inside your function could be the simple solution.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this a good and simple solution in these cases, an explanation for it would be good

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this even an idiom? I thought it is just a syntax?

Comment on lines +28 to +29
This allows using the terser syntax permitted by the [`?`] operator inside an
infallible context.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit repetitive, it's good for this, because it's good like. maybe a bit more elaboration. maybe the text from my last comments would fit better in here then


## Disadvantages

This is hiding failure states from the consumer of the API.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is that bad and when do you really want to avoid it? mentioning some cases would be good and probably also state, that if this is overused it could be considered an anti-pattern

@jRimbault
Copy link
Author

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.

fn file_stem(path: &Path) -> &str {
// this closure is fallible, so it can make use of `?`
// fn() -> Option<&str>
let inner = || Some(path.file_stem()?.to_str()?);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this example is not making full use of ?, it could even have TryInto IIRC.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be written :

let inner =  || path.file_stem()?.to_str();

but that wouldn't show much

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think that is better. I don't know why we need to have another layer for that.

@simonsan simonsan added the C-stale Category: An issue/a PR that hasn't been worked on in a longer time (usually 90 days) label Apr 6, 2023
@simonsan simonsan marked this pull request as draft April 7, 2023 20:31
@simonsan
Copy link
Collaborator

Any updates on this? 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-idiom Area: Idioms C-addition Category: Adding new content, something that didn't exist in the repository before C-stale Category: An issue/a PR that hasn't been worked on in a longer time (usually 90 days)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants