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

kirillrakhman

03/20/2017, 8:28 AM
are inline suspend parameters planned? e.g. when I want to execute some suspend lambda but want to wrap it in some
use
style code but don't want additional allocations.
e

elizarov

03/20/2017, 8:30 AM
k

kirillrakhman

03/20/2017, 8:31 AM
thx
e

elizarov

03/20/2017, 8:31 AM
k

kirillrakhman

03/20/2017, 8:32 AM
@elizarov actually, I was thinking of something slightly different:
Copy code
inline fun launchWithDismiss(block: suspend () -> Unit) = launch(handleExceptions = true) {
    try {
        block()
    } finally {
        dismissAllowingStateLoss()
    }
}
is this also covered by the issue?
e

elizarov

03/20/2017, 8:37 AM
That works now if you add “noinline” modifier before the
block
param. Should also work without “noinline” modifier if the issue I’ve quoted is fixed.
I’m add to the comments
k

kirillrakhman

03/20/2017, 8:39 AM
There's no difference in allocations between
inline
with
noinline
and leaving out
inline
altogether, right?
e

elizarov

03/20/2017, 8:40 AM
Nope.
Btw, if all you want to do is to handle exception inside
launch
, then you don’t need it.
You need to define your own
CoroutineExceptionHandler
k

kirillrakhman

03/20/2017, 8:42 AM
I know, that's not what I want to do. What I want to do is dismiss a dialog, no matter how the coroutine terminates
ah I could do this with a context too, right?
e

elizarov

03/20/2017, 8:42 AM
You cannot do this with a context, but I think there is a better way to do it with coroutines:
Copy code
val job = launch(context) { … doSomething ... }
launch(context + NonCancellable) { // !!! 
    job.await()
    dismissAllowingStateLoss()
}
k

kirillrakhman

03/20/2017, 8:44 AM
In which way is this better? allocation-wise? Or elegance-wise?
e

elizarov

03/20/2017, 8:44 AM
Elegance-wise, of course
k

kirillrakhman

03/20/2017, 8:45 AM
but isn't my code pretty readable itself?
e

elizarov

03/20/2017, 8:46 AM
Actually, my version does slightly different thing, which may make it nonappropriate for your needs
Your code invokes
dismiss
when the original code completes (including whatever other cleanup there was), while my code invokes
dismiss
as soon as the job is cancelled (it may invoke
dismiss
before
doSomething
code had terminated)
k

kirillrakhman

03/20/2017, 8:49 AM
yeah, I'll go with my version for now
d

deviant

03/20/2017, 9:25 AM
pretty dangerous. what if activity/fragment has been destroyed before job is done?
k

kirillrakhman

03/20/2017, 9:27 AM
@deviant this is a
DialogFragment
that has
retainInstance = true
the whole job of the class is to run some code no matter what the activity lifecycle
d

deviant

03/20/2017, 9:28 AM
ok then 🙂
k

kirillrakhman

03/20/2017, 9:29 AM
it's a pretty neat idiom, if I dare say so. the user has some visual feedback that some work is being done and I don't have to take care of lifecycle issues
4 Views