Advanced | Help | Encyclopedia
Directory


Name binding

In computer science, binding refers to the association of objects and implementations with names in a programming language so that those objects and implementations can be accessed by the names. An object's names are said to be "bound" to the object.

The simplest example is defining subprograms:

def sum (x, y)
  x + y
end

sum (2, 3)

In this Ruby code, an implementation returning the sum of given two inputs is bound to a name, sum.

Table of contents

Binding time

Because objects in computer programs are usually resident in the computer memory, binding time is almost the same as the time of allocation of memory space for objects. Bind can be done either at compile-time or link-time (static binding) or at run-time (dynamic binding). Also, scope rules might define binding time of objects. Local variables, for example, are usually bound at run-time while global variables at compile-time.

For example,

static int n;
int main ()
{
  n = 12;
  return n;
}

In this C code, a global variable n is bound to certain location in the memory of the computer.

Dynamic binding for polymorphism

In object-oriented programming, an object can respond to the same message with a different implementation. Dynamic binding, also called dynamic dispatch is a common solution for this problem.

For example there may be an object that contains the name (data) 'Socrates', and which is of the class (or type) Person.

Now suppose all Persons are mortal. In object oriented programming, we can say that the Person class must implement the Mortal interface, which contains the method die().

Persons and Plants die in different ways, for example Plants don't stop breathing. 'Dynamic binding' is the practice of figuring out which method to invoke at runtime. For example, if we write

void kill(Mortal m) {
  m.die();
}

it is not clear whether m is a Person or a Plant, and thus whether Plant.die() or Person.die() should be invoked on the object. With dynamic binding, the m object is examined at runtime, and the method corresponding to its actual class is invoked. (This implies that the actual representation of an object in memory is just its data and doesn't include the methods.)

Deep binding v.s. shallow binding

Deep binding and shallow binding are two ways to implement binding.

Deep binding is the most common implementation of bindings. Each environment consists of variables' names and values. The environments are linked according to the scoping rules. When a name is used in a procedure the environments are searched from the inner to the outer nesting level.

Shallow binding associates bindings for each variable, and then searches the bindings associated with the variable to find the binding for the current environment. The major advantage with this approach is that it reduces the search time for variables that are bound in the global environment.

In shallow binding, lexical variables mentioned in anonymous subroutines are whichever variables with the same names happen to be in scope when the subroutine is called. In deep binding, they are the same ones that were in scope when the subroutine was created.

The above contains the text from perlfaq7

See also








Links: Addme | Keyword Research | Paid Inclusion | Femail | Software | Completive Intelligence

Add URL | About Slider | FREE Slider Toolbar - Simply Amazing
Copyright © 2000-2008 Slider.com. All rights reserved.
Content is distributed under the GNU Free Documentation License.