Dynamic programming language


In computer science, a dynamic programming language is a class of high-level programming languages, which at runtime execute many common programming behaviours that static programming languages perform during compilation. These behaviors could include an extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system. Although similar behaviors can be emulated in nearly any language, with varying degrees of difficulty, complexity and performance costs, dynamic languages provide direct tools to make use of them. Many of these features were first implemented as native features in the Lisp programming language.
Most dynamic languages are also dynamically typed, but not all are. Dynamic languages are frequently referred to as scripting languages, although that term in its narrowest sense refers to languages specific to a given run-time environment.

Implementation

Eval

Some dynamic languages offer an eval function. This function takes a string parameter containing code in the language and executes it. If this code stands for an expression, the resulting value is returned. However, Erik Meijer and Peter Drayton suggests that programmers "use eval as a poor man's substitute for higher-order functions."

Object runtime alteration

A type or object system can typically be modified during runtime in a dynamic language. This can mean generating new objects from a runtime definition or based on mixins of existing types or objects. This can also refer to changing the inheritance or type tree, and thus altering the way that existing types behave.

Reflection

is common in many dynamic languages, and typically involves analysis of the types and metadata of generic or polymorphic data. It can, however, also include full evaluation and modification of a program's code as data, such as the features that Lisp provides in analyzing S-expressions.

Macros

A limited number of dynamic programming languages provide features which combine code introspection and eval in a feature called macros. Most programmers today who are aware of the term macro have encountered them in C or C++, where they are a static feature which is built in a small subset of the language, and are capable only of string substitutions on the text of the program. In dynamic languages, however, they provide access to the inner workings of the compiler, and full access to the interpreter, virtual machine, or runtime, allowing the definition of language-like constructs which can optimize code or modify the syntax or grammar of the language.
Assembly, C, C++, early Java, and Fortran do not generally fit into this category.

Example code

The following examples show dynamic features using the language Common Lisp and its Common Lisp Object System.

Computation of code at runtime and late binding

The example shows how a function can be modified at runtime from computed source code

; the source code is stored as data in a variable
CL-USER > ))
  • BEST-GUESS-FORMULA*
; a function is created from the code and compiled at runtime, the function is available under the name best-guess
CL-USER >
; the function can be called
CL-USER >
265.225
; the source code might be improved at runtime
CL-USER > )
; a new version of the function is being compiled
CL-USER >
; the next call will call the new function, a feature of late binding
CL-USER >
16.28573

Object runtime alteration

This example shows how an existing instance can be changed to include a new slot when its class changes and that an existing method can be replaced with a new version.

; a person class. The person has a name.
CL-USER > ))
; a custom printing method for the objects of class person
CL-USER >

))
; one example person instance
CL-USER >
; the class person gets a second slot. It then has the slots name and age.
CL-USER > ))
; updating the method to print the object
CL-USER >

)))
; the existing object has now changed, it has an additional slot and a new print method
CL-USER > *person-1*
; we can set the new age slot of instance
CL-USER >
25
; the object has been updated
CL-USER > *person-1*

Assembling of code at runtime based on the class of instances

In the next example, the class person gets a new superclass. The print method gets redefined such that it assembles several methods into the effective method. The effective method gets assembled based on the class of the argument and the at runtime available and applicable methods.

; the class person
CL-USER > ))
; a person just prints its name
CL-USER >

))
; a person instance
CL-USER >
  • PERSON-1*
; displaying a person instance
CL-USER > *person-1*
; now redefining the print method to be extensible
; the around method creates the context for the print method and it calls the next method
CL-USER >

))
; the primary method prints the name
CL-USER >
)
; a new class id-mixin provides an id
CL-USER > ))
; the print method just prints the value of the id slot
CL-USER >
)
; now we redefine the class person to include the mixin id-mixin
CL-USER 241 > ))
; the existing instance *person-1* now has a new slot and we set it to 42
CL-USER 242 >
42
; displaying the object again. The print-object function now has an effective method, which calls three methods: an around method, the primary method and the after method.
CL-USER 243 > *person-1*

Examples

Popular dynamic programming languages include JavaScript, Python, Ruby, PHP, Lua and Perl. The following are generally considered dynamic languages: