4 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 bmitc 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?
- aardvark179 4y agoIt does seem hard to explain to people, and although I agree it is not normally the right answer, it is often a better answer than simple thread local variables.
- 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.
- CoastalCoder 4y agoI'm surprised that dynamic vs. lexical scope is hard for seasoned programmers / computer-scientists to grasp. Maybe it's because the two concepts are inter-related during program execution? Similar to the way that latency vs. bandwidth can confuse some people, because they're inter-related in some circumstances but not others?
- slaymaker1907 4y agoIt is definitely useful for things like semi-global options like a variable for the current stdout.