https://kotlinlang.org logo
Title
d

diesieben07

08/06/2018, 10:00 PM
val a: Deferred<Boolean> = TODO()
val b: Deferred<Boolean> = TODO()
val result = async {
    a.await() || b.await()
}
a

Alowaniak

08/06/2018, 10:07 PM
won't this wait for a first, then b though? Instead of short circuiting when b returns true before a?
d

diesieben07

08/06/2018, 10:12 PM
oh, that is true. you would have to do two async's then
Actually, that is not true. Not quite sure how you would do it with short-circuit tbh
j

jw

08/06/2018, 10:40 PM
you need a
select
g

groostav

08/07/2018, 1:46 AM
@diesieben07 to elaborate on what jake said: https://gist.github.com/Groostav/ac3f4090199153b2db94fa2016b20852
I think one of the problems with short circuiting as such is that
Deferreds
are eager by default, in other words, when you write
async
, we dont create a lazy coroutine by default. In other words, by expressing
right
as a Deferred, its already running, so you've already lost the point of short-circuiting typically. If you chose to express it as a
suspend () -> Boolean
though...
b

bdawg.io

08/07/2018, 5:41 PM
If you consider
val a: Boolean
val b: Boolean
a || b
,
a
is evaluated first and THEN
b
, which the results happen to be available already so it can immediately evaluate. It seems inconsistent that a
Deferred
variant would potentially evaluate
b
first instead of awaiting the value of
a