https://kotlinlang.org logo
#language-evolution
Title
# language-evolution
a

Animesh Sahu

01/15/2021, 4:10 AM
What's the recommended way to handle async object initialization? Since there are no suspend constructor, maybe a fake constructor (
suspend operator fun invoke
in the companion), or probably just wrap the result into a
Deferred<>
and call await() every time when needed?
r

rnett

01/15/2021, 5:23 AM
I've been using the fake constructor route without issue, although it is a bit annoying
👀 1
b

Ben Woodworth

01/15/2021, 5:27 PM
Instead of
invoke
, I prefer to make a top level function next to the class with the same name. The IDE seems to suggest this function, but won't suggest the
invoke
operator function.
1
👀 1
g

gildor

01/20/2021, 8:05 AM
just wrap the result into a 
Deferred<>
 and call await() every time
Doesn’t look as a good solution for me, it’s constructor after all, it creates all kind limitation when you need multiple instances, and just doesn’t work as constructor Same as Ben I usually use builder function with the same name as class (and make constructor private) or just builder function with custom name
👀 1
I would even argue that builder function is better solution in general, even if somehow suspend constructor would be allowed
👍 2
a

Animesh Sahu

01/20/2021, 8:10 AM
Thank you all for your insights 👏😁
2 Views