why can't initializers be `suspend` ?
# coroutines
l
why can't initializers be
suspend
?
s
You mean constructors?
l
yes
a
Cause of backward compatibility, more hackintosh solution could be using invoke
Copy code
class MyClass private constructor(params) {
	companion object {
		suspend operator fun invoke(params): MyClass {
            // do your suspension here
			return MyClass(params)
		}
	}
}
💯 2
s
Good question. Not entirely sure, but having objects only partially constructed while a thread goes off and do something else feels odd to me 🙂 You can launch/async parallel code in constructors, though.
☝️ 1
a
Call it just like any other constructor
s
And what @Animesh Sahu showed is a nice workaround 🙂, by using a factory method