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

LastExceed

02/26/2020, 2:10 PM
why can't initializers be
suspend
?
s

streetsofboston

02/26/2020, 2:11 PM
You mean constructors?
l

LastExceed

02/26/2020, 2:12 PM
yes
a

Animesh Sahu

02/26/2020, 2:14 PM
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

streetsofboston

02/26/2020, 2:14 PM
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

Animesh Sahu

02/26/2020, 2:15 PM
Call it just like any other constructor
s

streetsofboston

02/26/2020, 2:15 PM
And what @Animesh Sahu showed is a nice workaround 🙂, by using a factory method
4 Views