static scoping and dynamic scoping
来源:互联网 发布:unity3d encodetojpg 编辑:程序博客网 时间:2024/06/07 09:23
Static Scoping:
With static scope, a variable always refers to its top-level environment. This is a property
of the program text and unrelated to the runtime call stack. Because matching a variable
to its binding only requires analysis of the program text, this type of scoping is sometimes
also called lexical scoping. Static scope is standard in modern functional languages such as
ML because it allows the programmer to reason as if variable bindings are carried out by
substitution.
Static scoping also makes it much easier to make modular code and reason about it, since
its binding structure can be understood in isolation. In contrast, dynamic scope forces the
programmer to anticipate all possible dynamic contexts in which the module's code may
be invoked.
In other words, a language uses static scope or lexical scope if it is possible to determine
the scope of a declaration by looking only at the program. Otherwise, the language uses
dynamic scope
Dynamic Scoping:
With dynamic scope, each identifier has a global stack of bindings. Introducing a local
variable with name x pushes a binding onto the global x stack (which may have been
empty), which is popped off when the control flow leaves the scope. Evaluating x in any
context always yields the top binding. In other words, a global identifier refers to the
identifier associated with the most recent environment. Note that this cannot be done at
compile time because the binding stack only exists at runtime, which is why this type of
scoping is called dynamic scoping.
Static Scope:
$x = 100;
sub foo()
{
my $x = 10;
goo();
print "the first x is: $x\n";
}
sub goo()
{
my $x = 20;
hoo();
}
sub hoo()
{
my $x = 30;
print "the second x is: $x\n";
}
foo();
print "the third one is: $x\n";
输出结果:
the second x is: 30
the first x is:10
the third one is: 100
$x = 100;
sub foo()
{
my $x = 10;
goo();
print "the first x is: $x\n";
}
sub goo()
{
$x = 20;
hoo();
}
sub hoo()
{
$x = 30;
print "the second x is: $x\n";
}
foo();
print "the third one is: $x\n";
输出结果:
the second x is: 30
the first x is:10
the third one is: 30
Dynamic Scope:
$x = 100;
sub foo()
{
local $x = 10;
goo();
print "the first x is: $x\n";
}
sub goo()
{
$x = 20;
hoo();
}
sub hoo()
{
$x = 30;
print "the second x is: $x\n";
}
foo();
print "the third one is: $x\n";
输出结果:
the second x is: 30
the first x is:30
the third one is: 100
- static scoping and dynamic scoping
- Lexical Scoping 和 Dynamic Scoping
- JavaScript Scoping and Hoisting
- JavaScript Scoping and Hoisting
- CHAPTER 5 References and Scoping
- Emulating dynamic scoping in GNU R
- Scoping & Hoisting
- R Programming week2 Functions and Scoping Rules
- Scoping.py源代码分析
- R’s Scoping
- Day1-4.Scoping
- [Elasticsearch] 聚合作用域(Scoping Aggregations)
- [Elasticsearch] 聚合作用域(Scoping Aggregations) 4
- # 函数作用域之理解Scoping & Hoisting
- ES权威指南_04_aggs_05 Scoping Aggs
- Static and dynamic libraries
- static and dynamic
- name lookup of `i' changed for new ISO `for' scoping
- Ext4+Servlet+HTML5实现实时获取文件上传进度、本地预览、文件拖拽上传的相册实例
- 手把手教你使用Git
- Java解析和修改xml文件
- Android网络通信框架Volley使用技巧汇总(一)
- unity3d 嵌入 移动MM SDK 闪退的问题
- static scoping and dynamic scoping
- flash大小选择和添加mtd的data分区 -- linux内核
- [Hibernate Search] (1) 初识Hibernate Search
- 音视频的一些基础的参数及专业名词详解
- hive优化总结
- Android系统架构剖析(一)
- iOS开发一行代码系列:一行搞定输入框
- Oracle动态SQL语句
- 第10周项目3-血型统计