Travis Griggs
08/01/2023, 6:09 PMfun main() = runBlocking {
launch { foo("a", 2) }
launch { foo("b", 3) }
}
I get a "No main method found in project" error. But if I add a println
statement then things work?
fun main() = runBlocking {
launch { foo("a", 2) }
launch { foo("b", 3) }
println("tada")
}
I can put that anywhere (top, middle, last) for it to make it so I don't get the "No main method" error. If I try a different statement, such as just a 3 + 4
, I get the same error. But if I store it in a variable (e.g. val total = 3 + 4
) , then it works.
What's going on here? Is this something to do with the Playground? or a nuance of runBlocking
? Or something else?Youssef Shoaib [MOD]
08/01/2023, 6:12 PMlaunch
returns a Job
object, which allows you to manually cancel that launched coroutine. runBlocking
returns whatever your block returns. So you end up with a fun main(): Job
, which won't run since the expectation is that your main method returns Unit
. println
returns Unit. What you can do is simply annotate it fun main(): Unit = runBlocking {}
Nothing to do with coroutines and all to do with type inference and expression blocks for functionsJavier
08/01/2023, 6:14 PMsuspend fun main
which I think it is sugar to the run blocking methodYoussef Shoaib [MOD]
08/01/2023, 6:15 PMsuspend fun main
is ever so slightly different than runBlocking
(it doesn't even rely on coroutines lol) but I think it is considered "better" than runBlocking
Travis Griggs
08/01/2023, 6:38 PMCLOVIS
08/01/2023, 8:36 PM