This message was deleted.
# coroutines
s
This message was deleted.
y
I highly and severely doubt the
get
is getting removed. Re: harmful: it's because you're assuming a very specific type. In most situations, the interceptor is indeed a dispatcher, but third party libs can mess that up (I've myself written an interceptor that isn't a dispatcher because it was way easier). It's also giving you access to methods that you maybe shouldn't use. The mechanism they're removing is polymorphic keys
My guess is that any usage of polymorphic keys is getting removed, so any key that was a "sub-key" will get removed or something like that, but the primary keys in the hierarchy stay (things like interceptor) A
Key<T>
will always give you whatever value is in the context, be it some
U : T
or not. What's getting removed is the polymorphic keys that would do a cast in addition to retrieving the element from the context, thus surfacing the cast to the user directly
All seems correct! The "`U` is not a `CoroutineContext.Element`" bit confused me at first until I realized what you were doing! That's a very "academic" way to phrase that (positive) Importantly, your second point about
TFound
is just Liskov substitution. In fact, finding an interceptor would be nonsensical otherwise since it's an interface, so you can't have an object
myInterceptor
where
myInterceptor:: class == ContinuationInterceptor::class
. Personally, I found polymorphic keys to be confusing, especially if you're writing your own coroutine context element, so I'm happy they're going away.
d
Hi!
But what did they mean by ‘as well as the standard library mechanism supporting all this’?
We meant kotlinlang.org/api/core/…/-abstract-coroutine-context-key .
CoroutineContext
will eventually ditch the
operator fun get(Key<E>): E?
?
Nope,
CoroutineContext
itself stays untouched!
What about a
ContinuationInterceptor
that’s used
as?
a
CoroutineDispatcher: ContinuationInterceptor
is ‘too specific’?
Consider this piece of code:
Copy code
val x: List<Int> = listOf(1, 2, 3)
println((x as MutableList<Int>)[0])
MutableList
is a more specific type than
List<Int>
.
What about the cast is ‘unnecessary and even harmful’?
Continuing with the
list
example above,
list[0]
would work even without the type cast, so
as MutableList<Int>
is useless. Moreover,
as MutableList<Int>
throws an exception if
x
isn't a mutable list, introducing a new failure mode for no benefit.
Are they just arguing that many functions for example that take a
CoroutineDispatcher
should have been written to take a
ContinuationInterceptor
instead? Their hope is that requiring the
as? CoroutineDispatcher
cast will help people realise in situations where it’s not required that they can forgo it?
Precisely!