Dynamic programming language

Last updated

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.

Contents

Most dynamic languages are also dynamically typed, but not all are. Dynamic languages are frequently (but not always) 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 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

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 (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 ]

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 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

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>(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>

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 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>

Examples

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

See also

Related Research Articles

<span class="mw-page-title-main">Common Lisp</span> Programming language standard

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.

<span class="mw-page-title-main">Dylan (programming language)</span> Multi-paradigm programming language

Dylan is a multi-paradigm programming language that includes support for functional and object-oriented programming (OOP), and is dynamic and reflective while providing a programming model designed to support generating efficient machine code, including fine-grained control over dynamic and static behaviors. It was created in the early 1990s by a group led by Apple Computer.

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.

Cocoa is Apple's native object-oriented application programming interface (API) for its desktop operating system macOS.

Prototype-based programming is a style of object-oriented programming in which behaviour 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 computer programming, the scope of a name binding is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts of the program, the name may refer to a different entity, or to nothing at all. Scope helps prevent name collisions by allowing the same name to refer to different objects – as long as the names have separate scopes. The scope of a name binding is also known as the visibility of an entity, particularly in older or more technical literature—this is in relation to the referenced entity, not the referencing name.

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.

<span class="mw-page-title-main">Common Lisp Object System</span>

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 programming languages, name binding is the association of entities with identifiers. An identifier bound to an object is said to reference that object. Machine languages have no built-in notion of identifiers, but name-object bindings as a service and notation for the programmer is implemented by programming languages. Binding is intimately connected with scoping, as scope determines which names bind to which objects – at which locations in the program code (lexically) and in which one of the possible execution paths (temporally).

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 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.

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 computing, late binding or dynamic linkage—though not an identical process to dynamically linking imported code libraries—is a computer programming mechanism in which the method being called upon an object, or the function being called with arguments, is looked up by name at runtime. In other words, a name is associated with a particular operation or object at runtime, rather than during compilation. The name dynamic binding is sometimes used, but is more commonly used to refer to dynamic scope.

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.

<span class="mw-page-title-main">EuLisp</span>

EuLisp is a statically and dynamically scoped Lisp dialect developed by a loose formation of industrial and academic Lisp users and developers from around Europe. The standardizers intended to create a new Lisp "less encumbered by the past", and not so minimalist as Scheme. Another objective was to integrate the object-oriented programming paradigm well. It is a third-generation programming language.

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 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, both hence their names.

<span class="mw-page-title-main">Object-oriented programming</span> Programming paradigm based on the concept of objects

Object-oriented programming (OOP) is a programming paradigm based on the concept of objects, which can contain data and code: data in the form of fields, and code in the form of procedures. In OOP, computer programs are designed by making them out of objects that interact with one another.

References

  1. Meijer, Erik and Peter Drayton (2005), Static Typing Where Possible, Dynamic Typing When Needed: The End of the Cold War Between Programming Languages (PDF), Microsoft Corporation, CiteSeerX   10.1.1.69.5966
  2. Chapter 24. Dynamic language support. Static.springsource.org. Retrieved on 2013-07-17.
  3. < "Groovy - Home". Archived from the original on 2014-03-02. Retrieved 2014-03-02.

Further reading

(Many use the term "scripting languages".)