What's the recommended way to handle async object ...
# language-evolution
a
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
I've been using the fake constructor route without issue, although it is a bit annoying
👀 1
b
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
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
Thank you all for your insights 👏😁