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

Avoid unsafe extraction in BindingReducer #3315

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

Conversation

stephencelis
Copy link
Member

@stephencelis stephencelis commented Aug 27, 2024

We currently have a single case of AnyCasePath(unsafe:) in the code base, which uses reflection for extraction, and is something we would like to remove in a future major version of Case Paths.

This explores a refactoring of BindingReducer that takes an explicit case key path to the binding action case, instead:

BindingReducer(action: \.binding)

BindableAction as a protocol is still necessary for bindable stores to know how to embed binding actions in a short syntax, e.g. $store.text.

It's a slight bummer that the reducer gets slightly more verbose, especially when there is something abstractable here, I'm just not sure Swift provides the tools yet to truly abstract over a case path in an ergonomic fashion.

The alternative would be to introduce a special protocol (BindableCasePath?) that the @Reducer macro would need to apply correctly when detecting a BindableAction, but this is one-off logic that seems to take things in the wrong direction.

This is a draft for now that does not clean up all the existing occurrences, as I'm not sure we want to complete and merge it.

We currently have a single case of `AnyCasePath(unsafe:)` in the code
base, which uses reflection for extraction, and is something we would
like to remove in a future major version of Case Paths.

This explores a refactoring of `BindingReducer` that takes an explicit
case key path toe the binding action case, instead:

```swift
BindingReducer(action: \.binding)
```

`BindableAction` as a protocol is still necessary for bindable stores to
know how to embed binding actions in a short syntax, _e.g._
`$store.text`.

It's a slight bummer that the reducer gets slightly more verbose,
especially when there _is_ something abstractable here, I'm just not
sure Swift provides the tools yet to truly abstract over a case path in
an ergonomic fashion.

The alternative would be to introduce a special protocol
(`BindableCasePath`?) that the `@Reducer` macro would need to apply
correctly when detecting a `BindableAction`, but this is one-off logic
that seems to take things in the wrong direction.
@stephencelis stephencelis requested a review from mbrandonw August 27, 2024 23:37
@stephencelis stephencelis marked this pull request as ready for review August 28, 2024 22:35
@stephencelis stephencelis marked this pull request as draft October 21, 2024 21:13
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.

1 participant