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

Remove the lambda parameters in behavior definition #18

Open
rcardin opened this issue Feb 5, 2024 · 0 comments
Open

Remove the lambda parameters in behavior definition #18

rcardin opened this issue Feb 5, 2024 · 0 comments
Labels
enhancement New feature or request
Milestone

Comments

@rcardin
Copy link
Owner

rcardin commented Feb 5, 2024

Embrace a more Kotlinish style, letting the KBehavior type be defined as follows:

object MainActor {
        val behavior: KkBehavior<Int> =
            setup {
                val counterRef = ctx.spawn("counter", Counter.behavior(0))

                counterRef `!` Counter.Increment(40)
                counterRef `!` Counter.Increment(2)
                ctx.log.info("Getting the value of the counter")
                counterRef `!` Counter.GetValue(ctx.self)
                counterRef `!` Counter.Reset
                counterRef `!` Counter.GetValue(ctx.self)

                receive {
                    ctx.log.info("The counter value is $msg")
                    same()
                }
            }
    }

object Counter {
        sealed interface Command

        data class Increment(val by: Int) : Command

        object Reset : Command

        data class GetValue(val replyTo: KActorRef<Int>) : Command

        fun behavior(currentValue: Int): KkBehavior<Command> =
            receive {
                when (msg) {
                    is Increment -> behavior(currentValue + msg.by)
                    is Reset -> behavior(0)
                    is GetValue -> {
                        msg.replyTo `!` currentValue
                        same()
                    }
                }
            }
    }

The ctx and msg should be implicitly available in the setup and receive factory methods.

@rcardin rcardin added the enhancement New feature or request label Feb 5, 2024
@rcardin rcardin added this to the 0.0.2 milestone Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant