关于运算符重载:Kotlin Coroutines中的“ +”?

“+” in Kotlin Coroutines?

这是Kotlin Coroutines通过显式作业取消的示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fun main(args: Array<String>) = runBlocking<Unit> {
    val job = Job() // create a job object to manage our lifecycle

    // now launch ten coroutines for a demo, each working for a different time
    val coroutines = List(10) { i ->
        // they are all children of our job object
        launch(coroutineContext + job) { // we use the context of main runBlocking thread, but with our own job object
            delay((i + 1) * 200L) // variable delay 200ms, 400ms, ... etc
            println("Coroutine $i is done")
        }
    }
    println("Launched ${coroutines.size} coroutines")
    delay(500L) // delay for half a second
    println("Cancelling the job!")
    job.cancelAndJoin() // cancel all our coroutines and wait for all of them to complete
}

我对表达式coroutineContext + job中的+感到困惑吗?

它在做什么? 是运营商覆盖吗?


这是运算符重载的一个示例。
下面显示了方法CoroutineContext::plus的文档:

1
open operator fun plus(context: CoroutineContext): CoroutineContext

Returns a context containing elements from this context and elements from other context. The elements from this context with the same key as in the other one are dropped.

它基本上是两种情况的合并。