Escopo (computação): diferenças entre revisões
Conteúdo apagado Conteúdo adicionado
Linha 85:
=== Escopo dinâmico ===
Com escopo dinâmico, cada identificador tem uma [[Pilha (informática)|pilha]] global de [[vinculação (computação)|vinculações]]. Introduzindo uma variável local com o nome de ''x'' empilha uma vinculação na pilha global ''x'' (que pode estar vazia), que estará desempilhada quando o fluxo de controle deixar o escopo. Avaliar ''x'' em qualquer contexto sempre produz a vinculação mais ao topo. Em outras palavras, um identificador global refere-se ao identificador associado com o ambiente mais recente. Note-se que isso não pode ser feito em tempo de compilação, porque a pilha de vinculação só existe em tempo de execução, razão pela qual este tipo de delimitação é chamado de ''escopo dinâmico''.
<!--
Generally, certain [[block (programming)|block]]s are defined to create bindings whose lifetime is the execution time of the block; this adds some features of static scoping to the dynamic scoping process. However, since a section of code can be called from many different locations and situations, it can be difficult to determine at the outset what bindings will apply when a variable is used (or if one exists at all). This can be beneficial; application of the [[principle of least knowledge]] suggests that code avoid depending on the ''reasons'' for (or circumstances of) a variable's value, but simply use the value according to the variable's definition. This narrow interpretation of shared data can provide a very flexible system for adapting the behavior of a function to the current state (or policy) of the system. However, this benefit relies on careful documentation of all variables used this way as well as on careful avoidance of assumptions about a variable's behavior, and does not provide any mechanism to detect interference between different parts of a program. As such, dynamic scoping can be dangerous and few modern languages use it. Some languages, like [[Perl]] and [[Common Lisp]], allow the programmer to choose static or dynamic scoping when defining or redefining a variable. [[Logo (programming language)|Logo]] and [[Emacs lisp]] are examples of languages that use dynamic scoping.
|