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++的参 ...
随机推荐
- 刀哥多线程之03GCD 常用代码
GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...
- jquery 小知识点
//计算checkbox有多少个被选中 $("input[name='user_apply']:checked").length)://可以查看 所有的name=user_appl ...
- Sql Server 语句
##目录 #####清除缓存 DBCC FREEPROCCACHE; DBCC DROPCLEANBUFFERS; SELECT stock.IdStock, stock.Descr FROM [In ...
- ios中怎么样判断路径最后的后缀名称
使用hasSuffix属性即可 例如 [photo.thumbnail_pic.lowercaseString hasSuffix:@"gif"]
- Javascript是一个事件驱动语言
面向原型这种说法我没在网上找到
- linux调整分区大小
查看一下当前分区情况 1 2 3 4 5 6 7 8 [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mou ...
- ifstat-网络接口监测工具
ifstat-网络接口监测工具 http://gael.roualland.free.fr/ifstat/ ifstat is a tool to report network interfaces ...
- JSON数组操作
在jquery中处理JSON数组的情况中遍历用到的比较多,但是用添加移除这些好像不是太多. 今天试过json[i].remove(),json.remove(i)之后都不行,看网页的DOM对象中好像J ...
- 树莓派最简易Wifi配置
树莓派最简易Wifi配置 相信我,连博客都会偷懒写个最简易给你看 前提,只有一根网线没有网络的前提下进行的. 基于Win10系统和树莓派2015-05-05-raspbian-wheezy.img测试 ...
- Qt 延时
第一部分: 关于sleep函数,我们先来看一下他的作用:sleep函数是使调用sleep函数的线程休眠,线程主动放弃时间片.当经过指定的时间间隔后,再启动线程,继续执行代码.sleep函数并不能起到定 ...