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

Adds a type argument for State #48

Merged
merged 2 commits into from
Jun 14, 2024
Merged

Conversation

Synesso
Copy link
Collaborator

@Synesso Synesso commented Jun 14, 2024

Adds a self type to State so that anyone extending State can define custom behaviour and have it available to them in any of places where kfsm references your state.

Prior to this change, it was not possible to complile the following:

sealed class Foo(to: () -> Set<State>) : app.cash.kfsm.State(to) {
  fun method() = this.subsequentStates.map { it.method() }
}

because subsequentStates was just a set of State, not a set of Foo.

Now, we can compile the following:

sealed class Foo(to: () -> Set<Foo>) : app.cash.kfsm.State<Foo>(to) {
  fun method() = this.subsequentStates.map { it.method() }
}

Because subsequentStates is a set of Foo

@Synesso Synesso enabled auto-merge (squash) June 14, 2024 05:20
Comment on lines +9 to +11
fun next(count: Int): List<Char> =
if (count <= 0) emptyList()
else subsequentStates.filterNot { it == this }.firstOrNull()?.let { listOf(it) + it.next(count - 1) } ?: emptyList()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Without this change, you would not be able to recursively call next here (or call it anywhere else in a callback for example)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The real test is that this compiles, but there is a test that uses this method also just to prove it out fully.

Copy link
Collaborator

@cwmyers cwmyers left a comment

Choose a reason for hiding this comment

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

bane

@Synesso Synesso merged commit f55ee6a into main Jun 14, 2024
2 checks passed
@Synesso Synesso deleted the jem/20240614-type-argument-for-state branch June 14, 2024 05:34
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