动态范围界定的优点是什么? [英] What are the advantages of dynamic scoping?

查看:125
本文介绍了动态范围界定的优点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,静态范围界定是唯一的明智方法做事,而动态范围是魔鬼的工具,其结果仅来自口译/编译器实施不力.

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 interpreters/compilers.

然后,我在 Common Lisp vs. Scheme 文章中看到了此摘要:

Then I saw this snippet from a Common Lisp vs. Scheme article:


Both Lexically and Dynamically    Lexical scope only, per the standard.
scoped special vars.  Common      Dynamically scoped vars are provided
Lisp just wins on this point.     by some implementations as an extension
                                  but code using them is not portable.

     (I have heard the arguments about whether Dynamic scoping
      is or is not a Bad Idea in the first place.  I don't care. 
      I'm just noting that you can do things with it that you 
      can't easily do without it.)

Common Lisp为什么会在这一点上取得胜利"?动态范围界定更容易做些什么?我真的无法证明是否需要它/将其视为一件好事.

Why does Common Lisp "just win on this point"? What things are easier to do with dynamic scoping? I really can't justify ever needing it / seeing it as a good thing.

推荐答案

像其他所有功能一样,动态作用域仅仅是一种工具.如果使用得当,它可以使某些任务变得更容易.使用不当会导致错误和头痛.

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.

-亚当

这篇关于动态范围界定的优点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆