# coroutines


12/24/2019, 9:58 PM
Has anyone built or seen an opensource example of coroutinecontext feature composition in action? the documentation of this feature is scarce, and not much comes from a github search. this seems like the right place for parsers to seat token sources and parse traits and work with less specialized generic components that can use the parts of context they require without elaborate function params.


12/24/2019, 11:37 PM
I'm not 100% get your use case, but yes, it looks that you can use context for this. kotlinx.coroutines has a bunch of examples, what kind example do you want to see?


12/25/2019, 4:12 AM
@gildor so far i have only seen coroutinecontext used upstream in the stdlib to define the main contexts. iiuc context is as close to threadlocal as kotlin will have for coroutines. as mentioned i saw no coroutinecontext usages in github at all. no parser mailboxes, no database transactions, no device drivers, no DI examples. coroutine contexts are not identical to threadlocals, the graph of instance applications of one pair supplying to its contexts can be many-to-many fanout. unless I am wrong about this. so lets take a usecase: I have a 55gigabyte file, and i want bytebuffers to occupy MAXINT regions with mmap. i can cheaply create a base coroutine context with
File + Channel +metadata
keys and then for each mmap region i can add the
ByeteBuffer + readOrWrite
element state. in combination with sealed class hierarchies it looks like context elements provide traits per context. according to traits were already a feature that was removed from early kotlin.