0

From https://en.wikipedia.org/wiki/Name_binding

name binding is the association of entities (data and/or code) with identifiers.[1] An identifier bound to an object is said to reference that object.

Binding is intimately connected with scoping, as scope determines which names bind to which objects – at which locations in the program code (lexically) and in which one of the possible execution paths (temporally).

  1. What is the difference between name binding and scoping?

    Is it correct that

    • name binding is to create a new binding between a name and an entity,
    • scoping is to find out which entity a name used somewhere in a program has been bound to?
  2. Are there relations between static and dynamic bindings, and static and dynamic scopings?

    For a name,

    • are its binding and scoping always either both dynamic or both static? If not, are they mostly?

    • For binding and scoping, does one being static or dynamic imply the other being static or dynamic?

Thanks.

p.s. I tried to look up name binding in my books, but didn't find them. Maybe the terminology is different in the books.

1 Answer 1

3

The quoted definitions of "entity" and "object" is ambiguous, we need to be more precise. A name is associated with a storage location which in turn contains a value. The value is the data. (I find it a bit odd to say the value could also be "code", but perhaps it refers to if the value is a function, which is possible.)

Scoping is how names are associated with storage locations. A name like x may refer to different storage locations in different parts of the program, but inside a single scope, the same name always refers to the same storage location.

function f() {
   int x = 17; // one storage location
}
function g() {
   string x = "Hello"; // a different storage location than the other x
}

But the value of a single storage location may change inside a scope (at least in languages which support destructive assignment, which is most mainstream languages).

function f() {
  int x = 17;
  x = 42; // the same storage location is assigned a new value
}

Static scoping means the scopes can be determined simply by looking at the code structure. Dynamic scoping means the scoping depends on which function calls which, which means the association between names and storage location are determined at runtime.

Most mainstream languages support only static scoping. I believe Common Lisp supports both static and dynamic scoping, in which case each name is either dynamically or statically scoped.

Dynamic binding (also known as late binding) can mean a few different things, but in this context it probably refers to whether the storage location for a name is located at compile-time or at runtime.

Dynamically scoped variables must by definition also be late bound, since they can refer to different storage locations during execution. But statically scoped variables can be either statically bound (like in C, Java) or late bound (like Pyton, JavaScript).

In a pure functional language (like Haskell) variables cannot change their value. This is conceptually simpler since you can think of a name as directly bound to a value in a scope. The same is the case for constants in imperative languages: In a statically bound imperative language, the compiler may skip generating storage locations for constants altogether, and instead replace all references to the name of the constant with the value itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.