HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, a dynamic programming language is a class of
high-level programming language In computer science, a high-level programming language is a programming language with strong abstraction from the details of the computer. In contrast to low-level programming languages, it may use natural language ''elements'', be easier to u ...
s, 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 In communications and information processing, code is a system of rules to convert information—such as a letter, word, sound, image, or gesture—into another form, sometimes shortened or secret, for communication through a communicati ...
, by extending
objects Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ai ...
and definitions, or by modifying the
type system In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
. 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 A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lispin ...
programming language. Most dynamic languages are also dynamically typed, but not all are. Dynamic languages are frequently (but not always) referred to as
scripting language A scripting language or script language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. Scripting languages are usually interpreted at runtime rather than compiled. A scripting ...
s, 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 or
abstract syntax tree In computer science, an abstract syntax tree (AST), or just syntax tree, is a tree representation of the abstract syntactic structure of text (often source code) written in a formal language. Each node of the tree denotes a construct occurr ...
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 In computer science, self-modifying code (SMC) is code that alters its own instructions while it is executing – usually to reduce the instruction path length and improve performance or simply to reduce otherwise repetitively similar code, ...
offered by eval from the
dynamic loading Dynamic loading is a mechanism by which a computer program can, at run time, load a library (or other binary) into memory, retrieve the addresses of functions and variables contained in the library, execute those functions or access those var ...
offered by
shared libraries In computer science, a library is a collection of non-volatile resources used by computer programs, often for software development. These may include configuration data, documentation, help data, message templates, pre-written code and ...
, and warn that in many cases eval is used merely to implement higher-order functions (by passing functions as strings) or deserialization.


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
mixin In object-oriented programming languages, a mixin (or mix-in) 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 depen ...
s of existing types or objects. This can also refer to changing the
inheritance Inheritance is the practice of receiving private property, titles, debts, entitlements, privileges, rights, and obligations upon the death of an individual. The rules of inheritance differ among societies and have changed over time. Of ...
or type tree, and thus altering the way that existing types behave (especially with respect to the invocation of
methods Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
).


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

Reflection Reflection or reflexion may refer to: Science and technology * Reflection (physics), a common wave phenomenon ** Specular reflection, reflection from a smooth surface *** Mirror image, a reflection in a mirror or in water ** Signal reflection, in ...
is common in many dynamic languages, and typically involves
analysis Analysis ( : analyses) is the process of breaking a complex topic or substance into smaller parts in order to gain a better understanding of it. The technique has been applied in the study of mathematics and logic since before Aristotle (3 ...
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 In computing, type introspection is the ability of a program to ''examine'' the type or properties of an object at runtime. Some programming languages possess this capability. Introspection should not be confused with reflection, which goes a ...
(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 Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
, and Fortran do not generally fit into this category.


Example code

The following examples show dynamic features using the language
Common Lisp Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fr ...
and its
Common Lisp Object System The Common Lisp Object System (CLOS) is the facility for object-oriented programming which is part of ANSI Common Lisp. CLOS is a powerful dynamic object system which differs radically from the OOP facilities found in more static languages suc ...
(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*) # ; 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*) # ; 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))) # ; 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)))) # ; one example person instance CL-USER > (setf *person-1* (make-instance 'person :name "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))) # ; 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)))) # ; 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 > (setf (slot-value *person-1* 'age) 25) 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 > (defclass person () ((name :initarg :name))) # ; a person just prints its name CL-USER > (defmethod print-object ((p person) stream) (print-unreadable-object (p stream :type t) (format stream "~a" (slot-value p 'name)))) # ; a person instance CL-USER > (defparameter *person-1* (make-instance 'person :name "Eva Luator")) *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 > (defmethod print-object :around ((p person) stream) (print-unreadable-object (p stream :type t) (call-next-method))) # ; the primary method prints the name CL-USER > (defmethod print-object ((p person) stream) (format stream "~a" (slot-value p 'name))) # ; a new class id-mixin provides an id CL-USER > (defclass id-mixin () ((id :initarg :id))) # ; the print method just prints the value of the id slot CL-USER > (defmethod print-object :after ((object id-mixin) stream) (format stream " ID: ~a" (slot-value object 'id))) # ; now we redefine the class person to include the mixin id-mixin CL-USER 241 > (defclass person (id-mixin) ((name :initarg :name))) # ; the existing instance *person-1* now has a new slot and we set it to 42 CL-USER 242 > (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-USER 243 > *person-1* #


Examples

Popular dynamic programming languages include
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
, Python,
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
, PHP, Lua and
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
. The following are generally considered dynamic languages: *
ActionScript ActionScript is an object-oriented programming language originally developed by Macromedia Inc. (later acquired by Adobe). It is influenced by HyperTalk, the scripting language for HyperCard. It is now an implementation of ECMAScript (meaning ...
*
BeanShell BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. It runs in the Java Runtime Environment (JRE), dynamically executes standard Java syntax and extends it with common scripting c ...
* C# (using Reflection) *
Clojure Clojure (, like ''closure'') is a dynamic and functional dialect of the Lisp programming language on the Java platform. Like other Lisp dialects, Clojure treats code as data and has a Lisp macro system. The current development process is comm ...
*
CobolScript CobolScript is a programming language created by Matthew Dean and Charles Schereda of Deskware in 1999. The language was intended to provide web-enabled COBOL COBOL (; an acronym for "common business-oriented language") is a compiled English ...
* ColdFusion Markup Language *
Common Lisp Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fr ...
and most other Lisps * Dylan * E *
Elixir ELIXIR (the European life-sciences Infrastructure for biological Information) is an initiative that will allow life science laboratories across Europe to share and store their research data as part of an organised network. Its goal is to bring t ...
* Erlang *
FORTH Forth or FORTH may refer to: Arts and entertainment * ''forth'' magazine, an Internet magazine * ''Forth'' (album), by The Verve, 2008 * ''Forth'', a 2011 album by Proto-Kaw * Radio Forth, a group of independent local radio stations in Scotla ...
* Gambas * GDScript * Groovy< * Java (using Reflection) *
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
* Julia * Lua *
MATLAB MATLAB (an abbreviation of "MATrix LABoratory") is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementat ...
/
Octave In music, an octave ( la, octavus: eighth) or perfect octave (sometimes called the diapason) is the interval between one musical pitch and another with double its frequency. The octave relationship is a natural phenomenon that has been refer ...
*
Objective-C Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style 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 NeXT ...
*
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
* PHP *
PowerShell PowerShell is a task automation and configuration management program from Microsoft, consisting of a command-line shell and the associated scripting language. Initially a Windows component only, known as Windows PowerShell, it was made open-sou ...
*
Prolog Prolog is a logic programming language associated with artificial intelligence and computational linguistics. Prolog has its roots in first-order logic, a formal logic, and unlike many other programming languages, Prolog is intended primarily ...
* Python * R * Raku * Rebol *
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
*
Smalltalk Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan ...
*
SuperCollider A particle accelerator is a machine that uses electromagnetic fields to propel charged particles to very high speeds and energies, and to contain them in well-defined beams. Large accelerators are used for fundamental research in particl ...
* Tcl *
VBScript VBScript (''" Microsoft Visual Basic Scripting Edition"'') is an Active Scripting language developed by Microsoft that is modeled on Visual Basic. It allows Microsoft Windows system administrators to generate powerful tools for managing comput ...
*
Wolfram Language The Wolfram Language ( ) is a general multi-paradigm programming language developed by Wolfram Research. It emphasizes symbolic computation, functional programming, and rule-based programming and can employ arbitrary structures and data. It ...


See also

* Comparison of programming languages * Name binding *
Von Neumann architecture The von Neumann architecture — also known as the von Neumann model or Princeton architecture — is a computer architecture based on a 1945 description by John von Neumann, and by others, in the '' First Draft of a Report on the EDVAC''. T ...


References


Further reading

*


External links

''(Many use the term "scripting languages".)'' * * * * * * ** ** {{DEFAULTSORT:Dynamic Programming Language Evaluation strategy