Skip to content

Instantly share code, notes, and snippets.

@RBusarow
Last active July 22, 2019 13:34
Show Gist options
  • Save RBusarow/4a3bc4d88bcc06c1805f70df66898246 to your computer and use it in GitHub Desktop.
Save RBusarow/4a3bc4d88bcc06c1805f70df66898246 to your computer and use it in GitHub Desktop.
Re-imagined CoroutineContext as a decorator around a standard map.
class CoroutineContext {
internal val map = mutableMapOf<Key<*>, Element>()
operator fun <E : Element> get(key: Key<E>): E? = map[key] as? E
operator fun plus(context: CoroutineContext): CoroutineContext {
val new = CoroutineContext()
new.map.putAll(map)
new.map.putAll(context.map)
return new
}
interface Key<E : Element>
interface Element
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment