Вопрос

I came across the expression top-level binding used within the context of dynamically typed languages and variable assignment.

What is meant by top level and how does this differ from normal lexical binding or dynamic binding?

Adding surrounding text for more context:

A variable can be bound to a value of any type. You can rebind mynum to a string:

mynum = ’Brown’

This binding lasts until you assign some other value to mynum...It is called a top-level binding. We will encounter cases of binding variables to values where the bindings are temporary.

Это было полезно?

Решение

Wikipedia had the following to say about a top-level binding on the page for Common Lisp:

In Common Lisp, a special variable which has only a top-level binding behaves just like a global variable in other programming languages. A new value can be stored into it, and that value simply replaces what is in the top-level binding.

A variable with top-level binding appears to be another name for a global variable.

The specific example you provided indicates it is in the outer-most/global scope of the program, as opposed to a smaller, temporary scope which goes away during execution. This variable would be available to any portion of the program assuming it had not been locally or temporarily overridden in the current lexical or dynamic context.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top