What are the advantages of dynamic scoping?

后端 未结 10 928
迷失自我
迷失自我 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:27

    Like everything else, Dynamic Scoping is merely a tool. Used well it can make certain tasks easier. Used poorly it can introduce bugs and headaches.

    I can certainly see some uses for it. One can eliminate the need to pass variables to some functions.

    For instance, I might set the display up at the beginning of the program, and every graphic operation just assumes this display.

    If I want to set up a window inside that display, then I can 'add' that window to the variable stack that otherwise specifies the display, and any graphic operations performed while in this state will go to the window rather than the display as a whole.

    It's a contrived example that can be done equally well by passing parameters to functions, but when you look at some of the code this sort of task generates you realize that global variables are really a much easier way to go, and dynamic scoping gives you a lot of the sanity of global variables with the flexibility of function parameters.

    -Adam

提交回复
热议问题