Skip to content

Instantly share code, notes, and snippets.

@ryohji
Created July 27, 2019 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryohji/fa1b900d16b898e73f778ee3784fc235 to your computer and use it in GitHub Desktop.
Save ryohji/fa1b900d16b898e73f778ee3784fc235 to your computer and use it in GitHub Desktop.
Kotlin/Native プロジェクト(サンプル)に Coroutine を追加する
diff --git a/build.gradle b/build.gradle
index 29bd433..6a89632 100644
--- a/build.gradle
+++ b/build.gradle
@@ -26,6 +26,9 @@ kotlin {
}
macosTest {
}
+ commonMain.dependencies {
+ implementation('org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.2.2')
+ }
}
}
diff --git a/settings.gradle b/settings.gradle
index fed354e..389c940 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -1,2 +1,3 @@
rootProject.name = 'native-hello'
+enableFeaturePreview('GRADLE_METADATA')
diff --git a/src/macosMain/kotlin/sample/SampleMacos.kt b/src/macosMain/kotlin/sample/SampleMacos.kt
index 1b82932..12189b0 100644
--- a/src/macosMain/kotlin/sample/SampleMacos.kt
+++ b/src/macosMain/kotlin/sample/SampleMacos.kt
@@ -1,7 +1,20 @@
package sample
+import kotlinx.coroutines.*
+
fun hello(): String = "Hello, Kotlin/Native!"
-fun main() {
- println(hello())
-}
\ No newline at end of file
+fun main() = runBlocking {
+ hello()
+ .map {
+ async {
+ delay(1000)
+ it
+ }
+ }
+ .awaitAll()
+ .joinToString("")
+ .let {
+ println(it)
+ }
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment