Skip to content

Remove the lambda parameters in behavior definition #18

@rcardin

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.

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