You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kotlin.adoc 1.7KB

12345678910111213141516171819202122232425262728293031323334353637
  1. +++
  2. +++
  3. == Kotlin
  4. https://kotlinlang.org/[Kotlin] is a modern language that can run on
  5. the JVM (as well as compiling to JavaScript or to native binaries).
  6. I use Kotlin for any Android app I write, and most desktop apps that
  7. require a GUI.
  8. === Assorted coroutine notes
  9. * *launched* in the context of a scope, e.g. `GlobalScope`
  10. * `launch` and `runBlocking` are both builders
  11. ** Can parameterise `runBlocking` for e.g. tests - `runBlocking<Unit> {...}`
  12. ** Builders return a job, can call `job.join()` to wait, `job.cancel()` to stop
  13. ** Every builder adds a `CoroutineScope` to the scope of a code block
  14. ** `launch`/`runBlocking` operate on current scope
  15. * `coroutineScope` is a builder to manually create a scope
  16. * `suspend` functions don't carry the scope
  17. ** keep a reference in the class/fun
  18. ** make them an extension method on CoroutineScope
  19. * Coroutines launched in `GlobalScope` don't keep the process alive if `main()` returns
  20. * Cancellation is co-operative - must throw `CancellationException`
  21. ** Can check `isAlive` property
  22. ** `withContext(NonCancellable)` if job *can't* be cancelled (e.g. suspend fun in a finally block)
  23. * `withTimeout` and `withTimeoutOrNull` cancel automatically
  24. * Children can be cancelled with `coroutineContext.cancelChildren()`
  25. * `async` is like `launch` but returns a `Deferred`
  26. * Contexts are job + dispatcher
  27. * Dispatcher specifies thread to run on
  28. * Launchers take an optional context
  29. ** `launch(newSingleThreadContext("name")) {...}`
  30. * `GlobalScope`'s dispatcher uses a shared threadpool
  31. * Can get own job from `CoroutineContext[Job]`
  32. * `CoroutineName` gives named context
  33. ** Can concat contexts with `+`
  34. * `select` can receive data from multiple channels, like Go