AbstractCoroutineContextKey.<init>
所在位置是kotlin.coroutines.AbstractCoroutineContextKey.<init>
,其相關用法介紹如下。
用法:
CoroutineContext.Key 的基類與多態CoroutineContext.Element 實現相關聯。多態元素實現意味著將其get和minusKey分別委托給getPolymorphicElement和minusPolymorphicKey。
可以使用元素鍵及其超類型鍵從協程上下文中提取多態元素。多態元素示例:
open class BaseElement : CoroutineContext.Element {
companion object Key : CoroutineContext.Key<BaseElement>
override val key: CoroutineContext.Key<*> get() = Key
// It is important to use getPolymorphicKey and minusPolymorphicKey
override fun <E : CoroutineContext.Element> get(key: CoroutineContext.Key<E>): E? = getPolymorphicElement(key)
override fun minusKey(key: CoroutineContext.Key<*>): CoroutineContext = minusPolymorphicKey(key)
}
class DerivedElement : BaseElement() {
companion object Key : AbstractCoroutineContextKey<BaseElement, DerivedElement>(BaseElement, { it as? DerivedElement })
}
// Now it is possible to query both `BaseElement` and `DerivedElement`
someContext[BaseElement] // Returns BaseElement?, non-null both for BaseElement and DerivedElement instances
someContext[DerivedElement] // Returns DerivedElement?, non-null only for DerivedElement instance
參數
safeCast
- 一個可以安全地轉換抽象的函數CoroutineContext.Element到混凝土E類型並返回元素,如果它是一個子類型E或者null
否則。
相關用法
- Kotlin AbstractCoroutineContextKey用法及代碼示例
- Kotlin Array.get用法及代碼示例
- Kotlin Array.set用法及代碼示例
- Kotlin associateBy用法及代碼示例
- Kotlin all用法及代碼示例
- Kotlin map用法及代碼示例
- Kotlin filterNot用法及代碼示例
- Kotlin reduceRight用法及代碼示例
- Kotlin Random.Default用法及代碼示例
- Kotlin Byte.inc用法及代碼示例
- Kotlin getValue用法及代碼示例
- Kotlin Double.dec用法及代碼示例
- Kotlin windowedSequence用法及代碼示例
- Kotlin contentToString用法及代碼示例
- Kotlin groupByTo用法及代碼示例
- Kotlin commonPrefixWith用法及代碼示例
- Kotlin MatchResult.Destructured用法及代碼示例
- Kotlin Delegates.notNull用法及代碼示例
- Kotlin ifBlank用法及代碼示例
- Kotlin filterNotTo用法及代碼示例
- Kotlin getOrPut用法及代碼示例
- Kotlin Triple.<init>用法及代碼示例
- Kotlin Duration.toString用法及代碼示例
- Kotlin windowed用法及代碼示例
- Kotlin indexOf用法及代碼示例
注:本文由純淨天空篩選整理自kotlinlang.org大神的英文原創作品 kotlin.coroutines.AbstractCoroutineContextKey.<init>。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。