Implementation
Eval
Some dynamic languages offer an eval function. This function takes a string or abstract syntax tree containing code in the language and executes it. If this code stands for an expression, the resulting value is returned. Erik Meijer and Peter Drayton distinguish the runtime code generation offered by eval from the dynamic loading offered by shared libraries and warn that in many cases eval is used merely to implement higher-order functions (by passing functions as strings) or deserialization.1
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 (especially with respect to the invocation of methods).
Type inference
As a lot of dynamic languages come with a dynamic type system, runtime inference of types based on values for internal interpretation marks a common task. As value types may change throughout interpretation, it is regularly used upon performing atomic operations.
Variable memory allocation
Static programming languages (possibly indirectly) require developers to define the size of utilized memory before compilation (unless working around with pointer logic). Consistent with object runtime alteration, dynamic languages implicitly need to (re-)allocate memory based on program individual operations.
Reflection
Reflective programming (reflection) is common in many dynamic languages, and typically involves analysis of the types and metadata of generic or polymorphic data. However, it can also include full evaluation and modification of a program's code as data, as in the features that Lisp provides in analyzing S-expressions.
Macros
A limited number of dynamic programming languages provide features which combine code introspection (the ability to examine classes, functions, and keywords to know what they are, what they do and what they know) 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.
The earliest dynamic programming language is considered to be Lisp (McCarthy, 1965) which continued to influence the design of programming languages to the present day.2
Example code
The following examples show dynamic features using the language Common Lisp and its Common Lisp Object System (CLOS).
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 > (defparameter *best-guess-formula* '(lambda (x) (* x x 2.5)))
*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 > (compile 'best-guess *best-guess-formula*)
#<Function 15 40600152F4>
; the function can be called
CL-USER > (best-guess 10.3)
265.225
; the source code might be improved at runtime
CL-USER > (setf *best-guess-formula* `(lambda (x) ,(list 'sqrt (third *best-guess-formula*))))
(LAMBDA (X) (SQRT (* X X 2.5)))
; a new version of the function is being compiled
CL-USER > (compile 'best-guess *best-guess-formula*)
#<Function 16 406000085C>
; the next call will call the new function, a feature of late binding
CL-USER > (best-guess 10.3)
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 > (defclass person () ((name :initarg :name)))
#<STANDARD-CLASS PERSON 4020081FB3>
; a custom printing method for the objects of class person
CL-USER > (defmethod print-object ((p person) stream)
(print-unreadable-object (p stream :type t)
(format stream "~a" (slot-value p 'name))))
#<STANDARD-METHOD PRINT-OBJECT NIL (PERSON T) 4020066E5B>
; one example person instance
CL-USER > (setf *person-1* (make-instance 'person :name "Eva Luator"))
#<PERSON Eva Luator>
; the class person gets a second slot. It then has the slots name and age.
CL-USER > (defclass person () ((name :initarg :name) (age :initarg :age :initform :unknown)))
#<STANDARD-CLASS PERSON 4220333E23>
; updating the method to print the object
CL-USER > (defmethod print-object ((p person) stream)
(print-unreadable-object (p stream :type t)
(format stream "~a age: ~" (slot-value p 'name) (slot-value p 'age))))
#<STANDARD-METHOD PRINT-OBJECT NIL (PERSON T) 402022ADE3>
; the existing object has now changed, it has an additional slot and a new print method
CL-USER > *person-1*
#<PERSON Eva Luator age: UNKNOWN>
; we can set the new age slot of instance
CL-USER > (setf (slot-value *person-1* 'age) 25)
25
; the object has been updated
CL-USER > *person-1*
#<PERSON Eva Luator age: 25>