context—Odin’s Most Misunderstood Feature
Even with the documentation on the topic, many people completely misunderstand what the context system is for, and what problem it actually solves.
For those not familiar with Odin, in each scope, there is an implicit value named context . This context variable is local to each scope and is implicitly passed by pointer to any procedure call in that scope (if the procedure has the Odin calling convention).
The main purpose of the implicit context system is for the ability to intercept third-party code and libraries and modify their functionality. One such case is modifying how a library allocates something or logs something. In C, this was usually achieved with the library defining macros which could be overridden so that the user could define what they wanted. However, not many libraries support this, in any language, by default which meant intercepting third-party code to see what it does and to change how it does it is generally not possible.
The context value has default values for its parameters which is decided in the package runtime. These defaults are compiler specific. To see what the implicit context value contains, please see the definition of the Context struct in package runtime.
The Misunderstanding
Fundamentally, the entire point of the context system is to intercept third-party code, and to change how it does things. By third-party, I just mean code not written by yourself or code that you cannot easily modify (which could even be your own past self’s code).
I expect most people to 100% ignore the context because its existence is not for whatever preconceived reason they think it is for, be that minimizing typing/passing things around, or dynamic scoping, etc. It’s just for interception of third-party code.
Ironically, context works because people misunderstand it, and thus generally leave it alone. That allows those who do understand it to work around less-than-ideal API’s.
I understand a lot of people may not understand why it exists when they might not currently need it, but it’s fundamentally a solution to a specific problem which cannot really be solved in another way.
... continue reading