Name binding
Encyclopedia
In programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

s, name binding is the association of objects (data and/or code) with identifier
Identifier
An identifier is a name that identifies either a unique object or a unique class of objects, where the "object" or class may be an idea, physical [countable] object , or physical [noncountable] substance...

s. An identifier bound to an object is said to reference
Reference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...

that object. Machine languages have no built-in notion of identifiers, but name-object bindings as a service and notation
Notation
-Written communication:* Phonographic writing systems, by definition, use symbols to represent components of auditory language, i.e. speech, which in turn refers to things or ideas. The two main kinds of phonographic notational system are the alphabet and syllabary...

for the programmer is implemented by programming languages. Binding is intimately connected with scoping
Scope (programming)
In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes. The type of scope determines what kind of entities it can contain and how it affects them—or semantics...

, 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).

Use of an identifier id in a context that establishes a binding for id is called a binding (or
defining) occurrence. In all other occurrences (e.g., in expressions, assignments, and subprogram calls), an identifier stands for what it is bound to; such occurrences are called applied occurrences.

Binding time

The binding of names before the program is run is called static (also "early"); bindings performed as the program runs are dynamic
Dynamic dispatch
In computer science, dynamic dispatch is the process of mapping a message to a specific sequence of code at runtime. This is done to support the cases where the appropriate method can't be determined at compile-time...

(also "late" or "virtual").

An example of a static binding is a direct C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 function call: the function referenced by the identifier cannot change at runtime.

But an example of dynamic binding is dynamic dispatch
Dynamic dispatch
In computer science, dynamic dispatch is the process of mapping a message to a specific sequence of code at runtime. This is done to support the cases where the appropriate method can't be determined at compile-time...

, as in a C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 virtual method call. Since the specific type of a polymorphic object is not known before runtime (in general), the executed function is dynamically bound. Take, for example, the following Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 code:


public void foo(List list) {
list.add("bar");
}


Is list a reference to a LinkedList, an ArrayList, or some other subtype
Subtype
In programming language theory, subtyping or subtype polymorphism is a form of type polymorphism in which a subtype is a datatype that is related to another datatype by some notion of substitutability, meaning that program constructs, typically subroutines or functions, written to operate on...

 of List? The actual method referenced by add is not known until runtime. In a language like C, the actual function is known.

Rebinding and mutation

Rebinding should not be confused with mutation
Mutation
In molecular biology and genetics, mutations are changes in a genomic sequence: the DNA sequence of a cell's genome or the DNA or RNA sequence of a virus. They can be defined as sudden and spontaneous changes in the cell. Mutations are caused by radiation, viruses, transposons and mutagenic...

 — "rebinding" is a change to the referencing identifier; "mutation" is a change to the referenced value. Consider the following Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 code:


LinkedList list;
list = new LinkedList;
list.add("foo");
list = null;


The identifier list initially references nothing (it is uninitialized); it is then rebound to reference an object (a linked list of strings). The linked list referenced by list is then mutated, adding a string to the list. Lastly, list is rebound to null.

Late static

Late static binding is a variant of binding somewhere between static and dynamic binding. Consider the following PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...

 example:

class A {
static $word = "hello";
static function hello {print self::$word;}
}

class B extends A {
static $word = "bye";
}

B::hello;


In this example, the PHP interpreter binds the function hello to class A, and so the call to B::hello produces the string "hello". If the semantics of "self::$word" had been based on "late static binding", then the result would have been "bye".

Beginning with PHP version 5.3, late static binding is supported http://us2.php.net/manual/en/language.oop5.late-static-bindings.php. Specifically, if "self::$word" in the above were changed to "static::$word" as shown in the following block, then the result of the call to B::hello would be "bye":


class A {
static $word = "hello";
static function hello {print static::$word;}
}

class B extends A {
static $word = "bye";
}

B::hello;

See also

  • Late binding
    Late binding
    Late binding is a computer programming mechanism in which the method being called upon an object is looked up by name at runtime. This is informally known as duck typing or name binding....

  • Branch table
    Branch table
    In computer programming, a branch table is a term used to describe an efficient method of transferring program control to another part of a program using a table of branch instructions. It is a form of multiway branch...

     method of applying Name binding via branch table or function pointers
  • Dynamic binding
    Dynamic dispatch
    In computer science, dynamic dispatch is the process of mapping a message to a specific sequence of code at runtime. This is done to support the cases where the appropriate method can't be determined at compile-time...

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK