What are the advantages of dynamic scoping?

后端 未结 10 929
迷失自我
迷失自我 2020-12-14 01:21

I\'ve learned that static scoping is the only sane way to do things, and that dynamic scoping is the tool of the devil, and results only from poor implementations of interpr

10条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 01:39

    Dynamically scoped variables are a powerful, but also sometimes unintuive and dangerous tool.

    Imagine you want to have thread specific global variables, i.e. every thread has its own set of global variables. This can easily be done with dynamic scope. Just change the references to this variables at thread initialization.

    Or think about exceptions: they are dynamically scoped in most languages. If you had to build an exceptions system from scratch, you could easily do that with dynamically scoped variables.

提交回复
热议问题