Make examples runnable
* Also drop args from fun main
diff --git a/docs/basics.md b/docs/basics.md
index f994469..fb0adb8 100644
--- a/docs/basics.md
+++ b/docs/basics.md
@@ -5,8 +5,6 @@
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.$$1$$2
-
-import kotlinx.coroutines.*
-->
<!--- KNIT ../core/kotlinx-coroutines-core/test/guide/.*\.kt -->
<!--- TEST_OUT ../core/kotlinx-coroutines-core/test/guide/test/BasicsGuideTest.kt
@@ -43,10 +41,12 @@
Run the following code:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) {
+import kotlinx.coroutines.*
+
+fun main() {
GlobalScope.launch { // launch new coroutine in background and continue
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
@@ -92,10 +92,12 @@
It is easy to get lost which one is blocking and which one is not.
Let's be explicit about blocking using [runBlocking] coroutine builder:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) {
+import kotlinx.coroutines.*
+
+fun main() {
GlobalScope.launch { // launch new coroutine in background and continue
delay(1000L)
println("World!")
@@ -122,10 +124,12 @@
This example can be also rewritten in a more idiomatic way, using `runBlocking` to wrap
the execution of the main function:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> { // start main coroutine
+import kotlinx.coroutines.*
+
+fun main() = runBlocking<Unit> { // start main coroutine
GlobalScope.launch { // launch new coroutine in background and continue
delay(1000L)
println("World!")
@@ -149,6 +153,10 @@
This is also a way to write unit-tests for suspending functions:
+<!--- INCLUDE
+import kotlinx.coroutines.*
+-->
+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
@@ -169,16 +177,20 @@
Delaying for a time while another coroutine is working is not a good approach. Let's explicitly
wait (in a non-blocking way) until the background [Job] that we have launched is complete:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
val job = GlobalScope.launch { // launch new coroutine and keep a reference to its Job
delay(1000L)
println("World!")
}
println("Hello,")
job.join() // wait until child coroutine completes
+//sampleEnd
}
```
@@ -213,10 +225,12 @@
an outer coroutine (`runBlocking` in our example) does not complete until all the coroutines launched
in its scope complete. Thus, we can make our example simpler:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
+import kotlinx.coroutines.*
+
+fun main() = runBlocking { // this: CoroutineScope
launch { // launch new coroutine in the scope of runBlocking
delay(1000L)
println("World!")
@@ -240,10 +254,12 @@
complete. The main difference between [runBlocking] and [coroutineScope] is that the latter does not block the current thread
while waiting for all children to complete.
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
+import kotlinx.coroutines.*
+
+fun main() = runBlocking { // this: CoroutineScope
launch {
delay(200L)
println("Task from runBlocking")
@@ -282,10 +298,12 @@
just like regular functions, but their additional feature is that they can, in turn,
use other suspending functions, like `delay` in this example, to _suspend_ execution of a coroutine.
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
launch { doWorld() }
println("Hello,")
}
@@ -322,7 +340,9 @@
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
repeat(100_000) { // launch a lot of coroutines
launch {
delay(1000L)
@@ -346,10 +366,13 @@
The following code launches a long-running coroutine in [GlobalScope] that prints "I'm sleeping" twice a second and then
returns from the main function after some delay:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
GlobalScope.launch {
repeat(1000) { i ->
println("I'm sleeping $i ...")
@@ -357,6 +380,7 @@
}
}
delay(1300L) // just quit after delay
+//sampleEnd
}
```