Static vs Dynamic Scope
3148 단어 dynamic
: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.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
API의 데이터가 포함된 Nuxt 2 동적 사이트맵일부 데이터 세트/api에서 사이트맵을 동적으로 구축하려는 경우 이것이 적합합니다. nuxt 프로젝트에서 익스프레스 API를 활성화했는지 여부에 관계없이 이 쉬운 3단계 프로세스를 통해 원하는 결과를 얻을 수 있습니...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.