Skip to content

Remove the lambda parameters in behavior definition #18

Open

Description

@rcardin

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.

Activity

  1. added this to the 0.0.2 milestone on Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions