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

Option to skip individual properties #436

Open
3 tasks
drewhamilton opened this issue Nov 18, 2024 · 0 comments
Open
3 tasks

Option to skip individual properties #436

drewhamilton opened this issue Nov 18, 2024 · 0 comments

Comments

@drewhamilton
Copy link
Owner

This has now been requested multiple times so I want to make it happen.

It is useful for consumers of Circuit and similar architectures, where the view model is a data class with a view event callback, e.g.:

data class CounterState(
    val count: Int,
    val eventSink: (CounterEvent) -> Unit,
) : CircuitUiState

A drawback of this is that as a lambda type, eventSink does not implement equals, and thus two instances of CounterState will never be equals. This can't be fixed with data classes, but it is possible to work around this in Poko by moving the property declaration of eventSink out of the constructor:

@Poko class CounterState(
    val count: Int,
    eventSink: (CounterEvent) -> Unit,
) : CircuitUiState {
    val eventSink = eventSink
}

But this is annoying to write, and it's not clear why the code is written this way to someone who doesn't already know.

In #182, @ZacSweers proposes a flag in the @Poko annotation to exclude lambda types from Poko's function generation. I think this would be too inflexible, and it would require Poko to maintain a list of types that this flag should apply to.

In the same PR, @JakeWharton proposes a new annotation applied to properties that Poko should skip. This is more verbose, but is more flexible and more explicit. The example above is simplified to this:

@Poko class CounterState(
    val count: Int,
    @PokoSkip val eventSink: (CounterEvent) -> Unit,
) : CircuitUiState

I'm in favor of this approach. It's pretty trivial to implement, but a few details need to be decided:

  • Annotation name: @PokoSkip, @PokoIgnore, @Skip, @Ignore, ...?
  • Do prospective consumers of this API need support for a custom annotation class, similar to that for @Poko?
  • The skipped property will be omitted from equals and hashCode completely. How should toString behave? My inclination was to omit it, but open to changing that.
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

No branches or pull requests

1 participant