| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
implicit-context is a DUB package implementing an implicit context system for D as a library.
A context is a table of named values, and each thread has a TLS stack of hierarchical contexts. It is similar to namespaces or environment variables, as the top-most name get the lookup.
It is a secondary stack for your program, to be used to pass "contextual" parameters like:
This system is inspired by Odin, Scala, and Jai, but without language support:
// Create or update myInt to value 8.
context.set!int("myInt", 8); Variable names MUST be valid D identifiers, else it's a programming error.
// Crash. "045" is an invalid identifier.
context.set!int("045", 1); // Retrieve value of myInt. This value is thread-local.
int myInt = context.get!int("myInt");When using context.get, an unknown variable identifier is a programming error and would crash:
int myInt = context.get!int("__non_Existing__"); // CrashUse context.query for an optional variable:
int myInt;
bool found = context.query!int("myInt", myInt);Note: wrong type size will crash. Type mismatch with right size will silently succeed, currently.
context.set!int("var", 4);
context.push;
context.set!int("var", 5); // mask former context variable
context.pop;
assert(context.get!int("var") == 4);How to allocate and reallocate:
// Allocate 1024 bytes.
void* p = context.allocator.malloc(1024);
// Change the allocation size to 24 bytes.
context.allocator.realloc(p, 24);
// Free the allocation.
context.allocator.free(p);| Back | FazBrowse Home | New Git URL |