This article has multiple issues. Please help improve it or discuss these issues on the talk page . (Learn how and when to remove these messages)
|
A dynamic programming language is a type of programming language that allows various operations to be determined and executed at runtime. This is different from the compilation phase. Key decisions about variables, method calls, or data types are made when the program is running, unlike in static languages, where the structure and types are fixed during compilation. Dynamic languages provide flexibility. This allows developers to write more adaptable and concise code.
For instance, in a dynamic language, a variable can start as an integer. It can later be reassigned to hold a string without explicit type declarations. This feature of dynamic typing enables more fluid and less restrictive coding. Developers can focus on the logic and functionality rather than the constraints of the language.
This section needs expansion. You can help by adding to it. (October 2009) |
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]
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).
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.
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 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.
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.[ clarification needed ]
The following examples show dynamic features using the language Common Lisp and its Common Lisp Object System (CLOS).
The example shows how a function can be modified at runtime from computed source code
; the source code is stored as data in a variableCL-USER>(defparameter*best-guess-formula*'(lambda(x)(*xx2.5)))*BEST-GUESS-FORMULA*; a function is created from the code and compiled at runtime, the function is available under the name best-guessCL-USER>(compile'best-guess*best-guess-formula*)#<Function1540600152F4>; the function can be calledCL-USER>(best-guess10.3)265.225; the source code might be improved at runtimeCL-USER>(setf*best-guess-formula*`(lambda(x),(list'sqrt(third*best-guess-formula*))))(LAMBDA(X)(SQRT(*XX2.5))); a new version of the function is being compiledCL-USER>(compile'best-guess*best-guess-formula*)#<Function16406000085C>; the next call will call the new function, a feature of late bindingCL-USER>(best-guess10.3)16.28573
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>(defclassperson()((name:initarg:name)))#<STANDARD-CLASSPERSON4020081FB3>; a custom printing method for the objects of class personCL-USER>(defmethodprint-object((pperson)stream)(print-unreadable-object(pstream:typet)(formatstream"~a"(slot-valuep'name))))#<STANDARD-METHODPRINT-OBJECTNIL(PERSONT)4020066E5B>; one example person instanceCL-USER>(setf*person-1*(make-instance'person:name"Eva Luator"))#<PERSONEvaLuator>; the class person gets a second slot. It then has the slots name and age.CL-USER>(defclassperson()((name:initarg:name)(age:initarg:age:initform:unknown)))#<STANDARD-CLASSPERSON4220333E23>; updating the method to print the objectCL-USER>(defmethodprint-object((pperson)stream)(print-unreadable-object(pstream:typet)(formatstream"~a age: ~"(slot-valuep'name)(slot-valuep'age))))#<STANDARD-METHODPRINT-OBJECTNIL(PERSONT)402022ADE3>; the existing object has now changed, it has an additional slot and a new print methodCL-USER>*person-1*#<PERSONEvaLuatorage:UNKNOWN>; we can set the new age slot of instanceCL-USER>(setf(slot-value*person-1*'age)25)25; the object has been updatedCL-USER>*person-1*#<PERSONEvaLuatorage:25>
let foo = 42; // foo is now a number foo = "bar"; // foo is now a string foo = true; // foo is now a boolean
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 personCL-USER>(defclassperson()((name:initarg:name)))#<STANDARD-CLASSPERSON4220333E23>; a person just prints its nameCL-USER>(defmethodprint-object((pperson)stream)(print-unreadable-object(pstream:typet)(formatstream"~a"(slot-valuep'name))))#<STANDARD-METHODPRINT-OBJECTNIL(PERSONT)40200605AB>; a person instanceCL-USER>(defparameter*person-1*(make-instance'person:name"Eva Luator"))*PERSON-1*; displaying a person instanceCL-USER>*person-1*#<PERSONEvaLuator>; now redefining the print method to be extensible; the around method creates the context for the print method and it calls the next methodCL-USER>(defmethodprint-object:around((pperson)stream)(print-unreadable-object(pstream:typet)(call-next-method)))#<STANDARD-METHODPRINT-OBJECT(:AROUND)(PERSONT)4020263743>; the primary method prints the nameCL-USER>(defmethodprint-object((pperson)stream)(formatstream"~a"(slot-valuep'name)))#<STANDARD-METHODPRINT-OBJECTNIL(PERSONT)40202646BB>; a new class id-mixin provides an idCL-USER>(defclassid-mixin()((id:initarg:id)))#<STANDARD-CLASSID-MIXIN422034A7AB>; the print method just prints the value of the id slotCL-USER>(defmethodprint-object:after((objectid-mixin)stream)(formatstream" ID: ~a"(slot-valueobject'id)))#<STANDARD-METHODPRINT-OBJECT(:AFTER)(ID-MIXINT)4020278E33>; now we redefine the class person to include the mixin id-mixinCL-USER241>(defclassperson(id-mixin)((name:initarg:name)))#<STANDARD-CLASSPERSON4220333E23>; the existing instance *person-1* now has a new slot and we set it to 42CL-USER242>(setf(slot-value*person-1*'id)42)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-USER243>*person-1*#<PERSONEvaLuatorID:42>
Popular dynamic programming languages include JavaScript, Python, Ruby, PHP, Lua and Perl. The following are generally considered dynamic languages:
Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ANSI INCITS 226-1994 (S2018). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived from the ANSI Common Lisp standard.
Ruby is an interpreted, high-level, general-purpose programming language. It was designed with an emphasis on programming productivity and simplicity. In Ruby, everything is an object, including primitive data types. It was developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan.
A visitor pattern is a software design pattern that separates the algorithm from the object structure. Because of this separation, new operations can be added to existing object structures without modifying the structures. It is one way to follow the open/closed principle in object-oriented programming and software engineering.
Self is a general-purpose, high-level, object-oriented programming language based on the concept of prototypes. Self began as a dialect of Smalltalk, being dynamically typed and using just-in-time compilation (JIT) with the prototype-based approach to objects: it was first used as an experimental test system for language design in the 1980s and 1990s. In 2006, Self was still being developed as part of the Klein project, which was a Self virtual machine written fully in Self. The latest version, 2024.1 was released in August 2024.
Prototype-based programming is a style of object-oriented programming in which behavior reuse is performed via a process of reusing existing objects that serve as prototypes. This model can also be known as prototypal, prototype-oriented,classless, or instance-based programming.
In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment. The environment is a mapping associating each free variable of the function with the value or reference to which the name was bound when the closure was created. Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.
The Common Lisp Object System (CLOS) is the facility for object-oriented programming in ANSI Common Lisp. CLOS is a powerful dynamic object system which differs radically from the OOP facilities found in more static languages such as C++ or Java. CLOS was inspired by earlier Lisp object systems such as MIT Flavors and CommonLoops, although it is more general than either. Originally proposed as an add-on, CLOS was adopted as part of the ANSI standard for Common Lisp and has been adapted into other Lisp dialects such as EuLisp or Emacs Lisp.
In computer programming, a generic function is a function defined for polymorphism.
In object-oriented programming languages, a mixin is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depends on the language. Mixins are sometimes described as being "included" rather than "inherited".
In computer science, reflective programming or reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.
In object-oriented programming, delegation refers to evaluating a member of one object in the context of another original object. Delegation can be done explicitly, by passing the responsibilities of the sending object to the receiving object, which can be done in any object-oriented language; or implicitly, by the member lookup rules of the language, which requires language support for the feature. Implicit delegation is the fundamental method for behavior reuse in prototype-based programming, corresponding to inheritance in class-based programming. The best-known languages that support delegation at the language level are Self, which incorporates the notion of delegation through its notion of mutable parent slots that are used upon method lookup on self calls, and JavaScript; see JavaScript delegation.
In some programming languages, eval
, short for the English evaluate, is a function which evaluates a string as though it were an expression in the language, and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval
. The input to eval
is not necessarily a string; it may be structured representation of code, such as an abstract syntax tree, or of special type such as code
. The analog for a statement is exec, which executes a string as if it were a statement; in some languages, such as Python, both are present, while in other languages only one of either eval
or exec
is.
In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter, which returns the value of the private member variable. They are also known collectively as accessors.
In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.
In computer programming, thread-local storage (TLS) is a memory management method that uses static or global memory local to a thread. The concept allows storage of data that appears to be global in a system with separate threads.
Magik is an object-oriented programming language that supports multiple inheritance and polymorphism, and it is dynamically typed. It was designed and implemented in 1989 by Arthur Chance of Smallworld Systems Ltd. as part of Smallworld Geographical Information System (GIS). Following Smallworld's acquisition in 2000, Magik is now provided by GE Energy, still as part of its Smallworld technology platform.
The circle–ellipse problem in software development illustrates several pitfalls which can arise when using subtype polymorphism in object modelling. The issues are most commonly encountered when using object-oriented programming (OOP). By definition, this problem is a violation of the Liskov substitution principle, one of the SOLID principles.
In computer science, marshalling or marshaling is the process of transforming the memory representation of an object into a data format suitable for storage or transmission, especially between different runtimes. It is typically used when data must be moved between different parts of a computer program or from one program to another.
In computer programming, a constant is a value that is not altered by the program during normal execution. When associated with an identifier, a constant is said to be "named," although the terms "constant" and "named constant" are often used interchangeably. This is contrasted with a variable, which is an identifier with a value that can be changed during normal execution. To simplify, constants' values remains, while the values of variables varies, hence both their names.
Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style message passing (messaging) to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTSTEP operating system. Due to Apple macOS’s direct lineage from NeXTSTEP, Objective-C was the standard language used, supported, and promoted by Apple for developing macOS and iOS applications from 1997, when Apple purchased NeXT until the introduction of the Swift language in 2014.
(Many use the term "scripting languages".)