Hi,
Ever had a bit of feature-envy about Odin's "context" feature
[1]? It is something used to pass "contextual" parameters, like a
logger, an allocator, to callees. It is akin to Scala's "implicit
parameters", or Jai contexts [2].
So I went ahead and implement a proof-of-concept library to have
scope globals in D with a TLS-based stack of environments. I went
ahead and implement a minimal logger, allocator, and "user
pointer" API on top of that context system.
It has a worse API and usability than a language solution but
basically I think there is no big blocker, if TLS and C runtime
are available to you.
DUB: https://code.dlang.org/packages/implicit-context
`implicit-context` is currently limited and
suggestions/requests/forks/destroying are much welcome.
As I see it, without compiler support the pluses are:
- no hidden "context" parameter in function calls, no new ABI
- contextual parameters are a bit like "scoped globals", they
will not change that often.
and the minuses are:
- manual push/pop
- wordy, opDispatch getter doesn't seem possible?
[1] https://odin-lang.org/docs/overview/#implicit-context-system
[2]
https://medium.com/@christoffer_99666/a-little-context-d06dfdec79a3