2 ms·
> Is dynamic scope hard to understand or be explained or isn't it just the case that most find it to be a terrible idea for general purpose software development
by eadmund 4y ago
> Is dynamic scope hard to understand or be explained or isn't it just the case that most find it to be a terrible idea for general purpose software development?
I think that dynamic scope is strictly better than global variables: all of its downsides are shared by globals, but it has the important advantage that a dynamic binding is scoped. Given that every popular language I am aware of supports globals in some form, I think that adopting dynamic scope would be an improvement.
Even more interesting is to consider what would it would mean to replace global with dynamic scoping entirely. I don’t really know if that is possible or efficient, but it certainly is intriguing. Almost like dependency injection throughout the language. Kinda sorta kinda.
- skybrian 4y agoI think Go's context objects serve a similar purpose to dynamic scope? https://pkg.go.dev/context https://pkg.go.dev/context It seems like pretty much anything that lets you pass in an environment as a function parameter would work.
- agumonkey 4y agoI believe dynamic variables are reinvented every year. Even dependency injection seems like a sibling.
- gpderetta 4y agoImplicit parameters seems to be the principled dynamic scoping for statically typed languages. Implicit parameters are in the list of must-have features for the programming language I'll never get around to implement.
- samatman 4y agoThis is effectively how globals are handled in Lua: they are first-class environments, ordinary tables which can be assigned to a function (5.1/LuaJIT) or upvalue (5.2+) to resolve globals. I like it a lot, it's a supplement to lexical scope: like ordinary globals it's a flat namespace 'behind' the locals, unlike many implementations of globals these environments are replaceable and (mostly) fungible.