Skip to content

Case key paths #121

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

Merged
merged 85 commits into from
Nov 12, 2023
Merged

Case key paths #121

merged 85 commits into from
Nov 12, 2023

Conversation

stephencelis
Copy link
Member

This branch reimagines case paths from first principles with the help of macros.

Given the new @CasePathable macro, one can annotate an enum:

@CasePathable
enum AppAction {
  case home(HomeAction)
  case settings(SettingsAction)
}

And get access to a bona fide key path for each case of the enum:

\AppAction.Cases.home  // CaseKeyPath<AppAction, HomeAction>

These case key paths can embed and extract values:

let appHomeCasePath = \AppAction.Cases.home

// Embed using function call syntax:
let action = appHomeCasePath(.onAppear)  // AppAction.home(.onAppear)

// Extract using a case subscript:
action[case: appHomeCasePath]  // Optional(HomeAction.onAppear)

// Extraction can fail:
action[case: \.settings]  // nil

And benefit from all the things key paths benefit from, like type inference, autocomplete, equatability, hashability, dynamic member lookup, and more.

Our libraries using case paths will be updated to support this new format soon. Be on the lookout!

@stephencelis stephencelis merged commit 40773cb into main Nov 12, 2023
@stephencelis stephencelis deleted the case-key-paths branch November 12, 2023 18:51
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