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

Added a convenience is function for optional CasePathable values #204

Merged
merged 3 commits into from
Oct 10, 2024

Conversation

HarshilShah
Copy link
Contributor

This PR is aimed at improving ergonomics for is checks specifically in TCA. Destinations and other pieces of CasePathable state can often be optional in TCA, and so out of the box the CasePathable.is function requires either unwrapping the optional state or tacking on == true to compile. I defined this extension in my own codebase a while ago to make that syntax a bit nicer to read, so as I was copying it over to another project I thought it might be worth submitting for inclusion right in the library itself

Copy link
Member

@stephencelis stephencelis left a comment

Choose a reason for hiding this comment

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

Hey @HarshilShah! Thanks for opening the PR as it made a deficiency clear to me 😄

Technically this API isn't actually needed, because Optional is CasePathable, and because you can squash \.some chaining when forming case key paths. You can confirm this by compiling the following today:

@CasePathable enum Foo { case bar }

func f() {
  let optional = Optional(Foo.bar)
  optional.is(\.bar)
}

I think the problem is that Xcode autocomplete will favor optional-chaining into the Wrapped over applying the method directly, so you get the following by default:

optional?.is(\.bar)

So I think this overload is worth adding to the library if only to make the autocomplete experience better 😄

Thanks again!

Sources/CasePaths/Optional+CasePathable.swift Outdated Show resolved Hide resolved
@stephencelis stephencelis merged commit ed5dd9a into pointfreeco:main Oct 10, 2024
7 checks passed
@HarshilShah HarshilShah deleted the optional branch October 11, 2024 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants