by mac3n · 2w ago · 0 comments
It's always bothered me that the only such feature in many languages is exception handlers (I'm also bothereed by functions that throw things).
I want "late" bindings, implemented as "shallow" bindings. "Late" as in binding is established at runtime in the call chain. "Shallow" as in you don't need to crawl the call chain to find it. Something like
extern Allocater *theAllocater = &DefaultAllocater;
...
// allocater scope
{ Allocater *prev = theAllocater;
theAllocater = NewAllocater();
...
theAllocater->free();
theAllocater = prev;
}
with a deep call using the current allocater // deep call
{ tmp = theAllocater->alloc(size);
...
}i have some generic command dispatch stuff that handles sudo/doas or other similar cli prefix wrapper utilities by just passing that info all the way down to the eventual external proc invocation as a dynamic variable (and actually it wouldn't even let me get away with sticking it all into the signatures because of potential argument conflicts in the general case). that way everything external this tool dispatches to can be defined as if run unprivileged for concision/flexibility but you still never need to invoke the tool itself with root privileges to sneak a sudo in for the eventual command because there's a mechanism for indicating desired privilege escalation that can quietly ripple out to everywhere it's needed without cluttering already complex signatures even more.
personally i find dynamic scoping immensely useful specifically in conjunction with exception handling: it can help you turn resumable exceptions into exceptions that return values to the thrown context in just a couple lines for e.g. an incredibly lightweight effect handling implementation or whatever you wanna do with that.
that said, without something readily visually distinguishing like raku's explicit twigils indicating a dynamic variable or something like clojure's conventional earmuffs on names to indicate the same, i do get why it's often seen as more trouble than it's worth by those less used to it.
why should everyone in the call chain need to know if some deep call allocates temporary storage.
So that you know how heavy it is!