https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

jkbbwr

04/11/2018, 11:45 AM
Is there a more spread out way of writing complex actors?
e

elizarov

04/11/2018, 1:25 PM
No yet
j

jkbbwr

04/12/2018, 12:19 PM
Something for you to consider its nice to have the pull rather than the push model sometimes.
e

elizarov

04/12/2018, 5:02 PM
We already have
actor { ... }
builder for pull if I correctly understand what you mean.
j

jkbbwr

04/13/2018, 2:25 AM
Copy code
fun acceptor() = actor<Message> {
    var ballot = -1
    var server = -1

    var acceptedBallot: Int? = null
    var acceptedValue: Any? = null

    fun handleProposal(message: Message.Propose) {
        // Did I promise to ignore this
        when {
            message.ballot < ballot -> {
                // Yea I promised to ignore this guy
                message.reply(null)
                return
            }
            message.ballot > ballot -> {
                // Yes they are, repromise to this person
                ballot = message.ballot
                message.reply(Message.Promise(
                    message.ballot,
                    acceptedBallot,
                    acceptedValue
                ))
                return
            }
            message.ballot == ballot -> {
                TODO()
            }
        }
    }

    fun handleFinalise(message: Message.Finalise) {
        // Did we promise to ignore this
        when {
            message.ballot < ballot -> {
                // Yea we are well past this
                message.reply(null)
            }
            message.ballot >= ballot -> {
                acceptedBallot = message.ballot
                acceptedValue = message.value
                message.reply(Message.Commit(
                    message.ballot,
                    message.value
                ))
            }
            else -> TODO("pid compare.")
        }
    }

    for (it in channel) {
        when(it) {
            is Message.Propose -> handleProposal(it)
            is Message.Finalise -> handleFinalise(it)
        }
    }
}
Is what I am currently having to do
3 Views