Encyclopedia > Dynamic typing

  Article Content

Dynamic typing

In programming languages, dynamic typing describes the practice of assigning types to each value in memory at runtime, rather than assigning a type to a static, syntactic entity in the program source code (the latter, contrasting practice is called static typing).

The implementation of a dynamically typed language will catch errors related to the misuse of values---"type errors"---at the time the erroneous statement or expression is computed. In other words, dynamic typing catches errors during program execution. A typical implementation of dynamic typing will keep all program values "tagged" with a type, and checking the type tag before any value is used in an operation.

For example, consider the following pseudocode:

 var x = 5;     // (1)
 var y = "hi";  // (2)
 x + y;         // (3)

In this code fragment, (1) binds the value 5 to x; (2) binds the value "hi" to y; and (3) attempts to add x to y. In a dynamically typed language implementation, the value bound to x might be a pair (integer, 5), and the value bound to y might be a pair (string, "hi"). When the program attempts to execute line (3), the language implementation would check the type tags integer and string, discover that the operation + (addition) is not defined over these two types, and signal an error.

Dynamic typing sometimes simplifies the task of writing code, because it allows the programmer to write code that would be illegal in some static type systems. Also, certain language constructs (for example, an eval function that can execute arbitrary data as code) are difficult to provide in a purely statically typed language.

However, purely dynamically typed languages provide only "late detection" of errors---errors may not be detected until the program is actually run. This complicates both the task of verifying that code is correct a priori, and the task of debugging code a posteriori when errors do arise. Dynamic typing advocates claim that the benefits of flexibility outweigh these disadvantages.

Well-known dynamically typed languages, in each of the major language paradigms, include the following:

Other dynamically typed languages include:

  • Ruby (a clean, pure-OO, interpreted language that gets out of the way and makes it easy to code in whatever style you deem appropriate.)
  • Python (a multi-paradigm language supporting all three styles)



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
KANU

... independence from British colonial rule in 1963. From October 1952 to December 1959, Kenya was under a state of emergency arising from the "Mau Mau" rebellion against ...

 
 
 
This page was created in 25.4 ms