Static vs Dynamic Scope
转自:http://hoolihan.net/blog-tim/2009/02/17/static-vs-dynamic-scope/
// start pseudo-code
var y = "global";
function print-y() {
print(y);
}
function test-scope() {
var y = "local";
print-y();
}
test-scope(); // statically scoped languages print "global"
// dynamically languages print "local"
print-y(); // all languages should print "global"
// end pseudo-code
This is the standard type of example used to explain what static scoping is as compared to dynamic scoping. This makes sense to me, but never really sank in.
To anyone who already gets this, this will seem trivial. But the lightbulb went off for me when I thought about static vs dynamic typing…
In a dynamically typed language (like ruby, javascript, etc), types are not checked until execution. If an expression evaluates, then the type-checking worked. If not, it blows up to your error handling or the user. Statically typed languages check types at compile time. The programmer ensures that parameter types are specified and the compiler ensures the programmers wishes will be followed.
Thinking in this fashion, static/dynamic scoping makes sense. For the following explanation, pretend that variables only have one type of storage for simplicity, and that global y is at memory location x01, while local y in test-scope is at x02.
If I’m a compiler in the act of compiling print-y (above code snippet) in a static language, then I know the scope I’m running in (hence static scope). I know that y is bound to the global variable, and I can replace that y with a direct location of x01 in the assembly I’m generating. No lookup tables, etc… fast.
If instead, I’m compling print-y in a dynamic scope, then I can make no such substitution. I’m going to make some calls to print-y that will point to x01 and others that point to x02. What y is bound to is determined by the scope of the call at runtime… which is the definition of dynamic scoping.
So that might help it click. Everything said about a stack in dynamic scoping is true, but I think it’s easier to understand that once you understand the above. Then you realize I could nest 4 or 5 of those calls and the last value of y would win.
So to sum up…
Static Scoping – Variables can be bound at compile time without regards to calling code.
Dynamic Scoping – Variable binding (context) can only be determined at the moment code is executed.
Emacs Lisp (elisp) is one of the few commonly used languages with dynamic scoping. It takes a lot of heat for that, as debugging and understanding the nuances involved place a greater burden on the developer. But I think it’s worth noting that in order to create an extensible system that can completely modify itself at runtime (via loading/reloading custom code) and allow extensions to modify extensions… the combination of a dynamic language and dynamic scoping has proved very successful.
Static vs Dynamic Scope的更多相关文章
- php-fpm进程管理方式(static和dynamic)
目前最新5.3.x的php-fpm,有两种管理进程的方式,分别是static和dynamic. 如果设置成static,进程数自始至终都是pm.max_children指定的数量,pm.start_s ...
- static binding/dynamic binding
static binding/dynamic binding class Person { private void who() { System.out.println("Inside p ...
- 浅谈游标选项 Static|Keyset|DYNAMIC|FAST_FORWARD
接好久之前太监的一篇Blog.现在补充几个选项的介绍 所用的语句都是这个 IF OBJECT_ID('T1') IS NOT NULL DROP TABLE T1 GO CREATE TABLE T1 ...
- Static, Shared Dynamic and Loadable Linux Libraries
转载:http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html Why libraries are used: Th ...
- C++ compile Microsoft Visual C++ Static and Dynamic Libraries
出处:http://www.codeproject.com/Articles/85391/Microsoft-Visual-C-Static-and-Dynamic-Libraries 出处:http ...
- ESSENTIALS OF PROGRAMMING LANGUAGES (THIRD EDITION) :编程语言的本质 —— (一)
# Foreword> # 序 This book brings you face-to-face with the most fundamental idea in computer prog ...
- Static Random-Access Memory Dynamic Random-Access Memory
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION RAM technology is div ...
- You Don't Know JS: Scope & Closures (附加:Lexical/dynamic作用域)(附加:Lexical-this)
JavaScript只有Lexical Scope 模式 Lexical Scope就是在写代码的时候,定义函数的时候创建的作用域! 而动态作用域是在runtime时,函数被调用的地方的作用域! 实际 ...
- Delphi DLL制作和加载 Static, Dynamic, Delayed 以及 Shared-Memory Manager
一 Dll的制作一般分为以下几步:1 在一个DLL工程里写一个过程或函数2 写一个Exports关键字,在其下写过程的名称.不用写参数和调用后缀.二 参数传递1 参数类型最好与window C++的参 ...
随机推荐
- MySQL 出现 Access denied for user 'root'@'localhost' (using password: YES) 错误
登录某台服务器的mysql时候总报错: mysql2/client.rb:58:in `connect': Access denied for user 'root'@'localhost' (usi ...
- 详谈 oracle 索引 (笔记)
1.oracle索引空值问题 当在有空值得列上建立单列索引时,如果搜索条件为 is null 在解释计划中可以看到,对于此列oracle并没有使用索引查询: 但是当建立的是多列索引是,就会按照索引来进 ...
- hdu 5444 Elven Postman
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Description Elves are very peculia ...
- 用Java实现3DES
3DES,即三重DES,是DES的加强版,也是DES的一个更安全的变形.它使用3个56位(共168位)的密钥对数据进行三次加密,和DES相比,安全性得到了较大的提高. 实际上,3DES是一个过渡的加密 ...
- iOS学习之C语言循环结构
一.while循环 while (循环条件) { 循环体: } // 1.定义循环变量 int time = 1; // 2.循环条件 while ( ...
- 在线演示平台 | Highcharts中文网 (曲线图、区域图、3D图等等)
http://www.hcharts.cn/ 在线演示平台 | Highcharts中文网
- Quartus13.0破解方法
一定要按照步骤顺序才能破解,这里很关键 1.下载和打开Quartus II破解器,选择“应用”,选择“是”,找到bin(64位系统是bin64)目录下的sys_cpt.dll,“打开” 2.然后将li ...
- 按键精灵实现自动退出的MsgBox消息框
要实现自动倒计时退出的消息框,代码如下: Set wsh = CreateObject("WScript.Shell") wsh.popup "设置完毕,3秒后自动退出! ...
- Notes of the scrum meeting(11/4)
meeting time:20:00~20:30p.m.,November 4th,2013 meeting place:20号公寓楼前 attendees: 顾育豪 ...
- 在Mac中如何显示和隐藏文件
1.显示Mac隐藏文件的命令: 在终端中输入"defaults write com.apple.finder AppleShowAllFiles YES":  鼠标单击窗口左上角 ...