AbstractCoroutineContextKey
所在位置是kotlin.coroutines.AbstractCoroutineContextKey
,其相关用法介绍如下。
用法:
@ExperimentalStdlibApi abstract class AbstractCoroutineContextKey<B : Element, E : B> :
Key<E>
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 AbstractCoroutineContextKey.<init>用法及代码示例
- 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用法及代码示例
注:本文由纯净天空筛选整理自kotlinlang.org大神的英文原创作品 kotlin.coroutines.AbstractCoroutineContextKey。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。