Jonathan
10/10/2018, 9:17 AMenleur
10/10/2018, 9:19 AMJonathan
10/10/2018, 9:20 AMsealed class Message
data class Ask(val question: Question, val response: CompletableDeferred<Response>)
suspend fun usage() {
val response = CompletableDeferred<Response>()
actor.send(Ask(Question(), response)) // ask
val result = response.await() // wait
// use the result
}
dave08
10/10/2018, 10:13 AMactWithResult { }
pattern missing there... but on one discussion with @[removed] it might not be so difficult to implement. I think the use case is running using a regular act { }
and getting results in another context using actWithResult { }
, or simply having a background context to call actWithResult { }
, the actor would insure that any requests will be handled synchronously...Jonathan
10/10/2018, 10:23 AMactAndReply
suspending function on top of act
. The problem is more about best and bad practices. @[removed] and @elizarov said that such pattern should be discouraged by kotlinx.coroutines, because it is an anti pattern (https://github.com/Kotlin/kotlinx.coroutines/pull/485#discussion_r208939367). So my question is more: How one can write simple actor composition and avoid this ant-pattern?dave08
10/10/2018, 10:23 AMJonathan
10/10/2018, 10:29 AMact
and actAndReply
in order to play with it. The thing is, since the ask & wait is considered an anti-pattern, I'd like to avoid it. But I struggle to compose actors without it.dave08
10/10/2018, 10:32 AMactAndReply
return Deferred
?Jonathan
10/10/2018, 10:37 AMsuspend fun foo(): Result
is equivalent and should be preferred to fun foo(): Deferred<Result>
.elizarov
10/10/2018, 10:38 AMJonathan
10/10/2018, 10:38 AMelizarov
10/10/2018, 10:39 AMJonathan
10/10/2018, 10:39 AMdave08
10/10/2018, 10:40 AMelizarov
10/10/2018, 10:42 AMdave08
10/10/2018, 10:42 AMelizarov
10/10/2018, 10:42 AMJonathan
10/10/2018, 10:43 AMelizarov
10/10/2018, 10:44 AMJonathan
10/10/2018, 10:48 AMelizarov
10/10/2018, 11:11 AMJonathan
10/10/2018, 11:28 AMelizarov
10/10/2018, 11:29 AMJonathan
10/10/2018, 11:29 AMelizarov
10/10/2018, 11:55 AMJonathan
10/10/2018, 12:20 PM