fbpx
Wikipedia

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

Eval edit

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 edit

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 edit

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 edit

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 edit

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 edit

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 edit

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 edit

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 edit

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> 

Assembling of code at runtime based on the class of instances edit

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))) #<STANDARD-CLASS PERSON 4220333E23> ; 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)))) #<STANDARD-METHOD PRINT-OBJECT NIL (PERSON T) 40200605AB> ; a person instance CL-USER > (defparameter *person-1* (make-instance 'person :name "Eva Luator")) *PERSON-1* ; displaying a person instance CL-USER > *person-1* #<PERSON Eva Luator> ; 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))) #<STANDARD-METHOD PRINT-OBJECT (:AROUND) (PERSON T) 4020263743> ; the primary method prints the name CL-USER > (defmethod print-object ((p person) stream)  (format stream "~a" (slot-value p 'name))) #<STANDARD-METHOD PRINT-OBJECT NIL (PERSON T) 40202646BB> ; a new class id-mixin provides an id CL-USER > (defclass id-mixin () ((id :initarg :id))) #<STANDARD-CLASS ID-MIXIN 422034A7AB> ; 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))) #<STANDARD-METHOD PRINT-OBJECT (:AFTER) (ID-MIXIN T) 4020278E33> ; now we redefine the class person to include the mixin id-mixin CL-USER 241 > (defclass person (id-mixin) ((name :initarg :name))) #<STANDARD-CLASS PERSON 4220333E23> ; 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* #<PERSON Eva Luator ID: 42> 

Examples edit

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

See also edit

References edit

  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. ^ < . Archived from the original on 2014-03-02. Retrieved 2014-03-02.

Further reading edit

  • Tratt, Laurence (2009). Dynamically Typed Languages. Advances in Computers. Vol. 77. pp. 149–184. doi:10.1016/s0065-2458(09)01205-4. ISBN 9780123748126.

External links edit

(Many use the term "scripting languages".)

  • Prechelt, Lutz (August 18, 2002). "Are Scripting Languages Any Good? A Validation of Perl, Python, Rexx, and Tcl against C, C++, and Java" (PDF). Advances in Computers. 57: 205–270. doi:10.1016/S0065-2458(03)57005-X. ISBN 9780120121571. ISSN 0065-2458. Retrieved 2020-07-27.
  • Bezroukov, Nikolai (2013). "A Slightly Skeptical View on Scripting Languages". Softpanorama (2.1 ed.). Retrieved 2020-07-27.
  • Wall, Larry (December 6, 2007). Programming is Hard, Let's Go Scripting... (Speech). State of the Onion 11. Perl.com. Retrieved 2020-07-27.
  • Roth, Gregor (November 20, 2007). "Scripting on the Java platform". JavaWorld. Retrieved 2020-07-27.
  • Ousterhout, John K. (March 1998). (PDF). Computer. Vol. 31, no. 3. pp. 23–30. doi:10.1109/2.660187. ISSN 0018-9162. Archived from the original (PDF) on 2020-07-27. Retrieved 2020-07-27.
  • "ActiveState Announces Focus on Dynamic Languages". ActiveState. July 26, 2004. Retrieved 2020-07-27.
    • Ascher, David (July 27, 2004). (PDF). Whitepapers. ActiveState. Archived from the original (PDF) on 2008-11-18.
    • Ascher, David (July 27, 2004). . Whitepapers. ActiveState. Archived from the original on 2008-12-08.

dynamic, programming, language, this, article, multiple, issues, please, help, improve, discuss, these, issues, talk, page, learn, when, remove, these, template, messages, this, article, factual, accuracy, disputed, relevant, discussion, found, talk, page, ple. This article has multiple issues Please help improve it or discuss these issues on the talk page Learn how and when to remove these template messages This article s factual accuracy is disputed Relevant discussion may be found on the talk page Please help to ensure that disputed statements are reliably sourced March 2012 Learn how and when to remove this message This article may be confusing or unclear to readers Please help clarify the article There might be a discussion about this on the talk page October 2009 Learn how and when to remove this message Learn how and when to remove this message 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 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 Contents 1 Implementation 1 1 Eval 1 2 Object runtime alteration 1 3 Type inference 1 4 Variable memory allocation 1 5 Reflection 1 6 Macros 2 Example code 2 1 Computation of code at runtime and late binding 2 2 Object runtime alteration 2 3 Assembling of code at runtime based on the class of instances 3 Examples 4 See also 5 References 6 Further reading 7 External linksImplementation editThis section needs expansion You can help by adding to it October 2009 Eval edit 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 edit 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 edit 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 edit 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 edit 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 edit 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 editThe 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 edit 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 gt 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 gt compile best guess best guess formula lt Function 15 40600152F4 gt the function can be called CL USER gt best guess 10 3 265 225 the source code might be improved at runtime CL USER gt 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 gt compile best guess best guess formula lt Function 16 406000085C gt the next call will call the new function a feature of late binding CL USER gt best guess 10 3 16 28573 Object runtime alteration edit 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 gt defclass person name initarg name lt STANDARD CLASS PERSON 4020081FB3 gt a custom printing method for the objects of class person CL USER gt defmethod print object p person stream print unreadable object p stream type t format stream a slot value p name lt STANDARD METHOD PRINT OBJECT NIL PERSON T 4020066E5B gt one example person instance CL USER gt setf person 1 make instance person name Eva Luator lt PERSON Eva Luator gt the class person gets a second slot It then has the slots name and age CL USER gt defclass person name initarg name age initarg age initform unknown lt STANDARD CLASS PERSON 4220333E23 gt updating the method to print the object CL USER gt 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 lt STANDARD METHOD PRINT OBJECT NIL PERSON T 402022ADE3 gt the existing object has now changed it has an additional slot and a new print method CL USER gt person 1 lt PERSON Eva Luator age UNKNOWN gt we can set the new age slot of instance CL USER gt setf slot value person 1 age 25 25 the object has been updated CL USER gt person 1 lt PERSON Eva Luator age 25 gt Assembling of code at runtime based on the class of instances edit 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 gt defclass person name initarg name lt STANDARD CLASS PERSON 4220333E23 gt a person just prints its name CL USER gt defmethod print object p person stream print unreadable object p stream type t format stream a slot value p name lt STANDARD METHOD PRINT OBJECT NIL PERSON T 40200605AB gt a person instance CL USER gt defparameter person 1 make instance person name Eva Luator PERSON 1 displaying a person instance CL USER gt person 1 lt PERSON Eva Luator gt 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 gt defmethod print object around p person stream print unreadable object p stream type t call next method lt STANDARD METHOD PRINT OBJECT AROUND PERSON T 4020263743 gt the primary method prints the name CL USER gt defmethod print object p person stream format stream a slot value p name lt STANDARD METHOD PRINT OBJECT NIL PERSON T 40202646BB gt a new class id mixin provides an id CL USER gt defclass id mixin id initarg id lt STANDARD CLASS ID MIXIN 422034A7AB gt the print method just prints the value of the id slot CL USER gt defmethod print object after object id mixin stream format stream ID a slot value object id lt STANDARD METHOD PRINT OBJECT AFTER ID MIXIN T 4020278E33 gt now we redefine the class person to include the mixin id mixin CL USER 241 gt defclass person id mixin name initarg name lt STANDARD CLASS PERSON 4220333E23 gt the existing instance person 1 now has a new slot and we set it to 42 CL USER 242 gt 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 gt person 1 lt PERSON Eva Luator ID 42 gt Examples editPopular dynamic programming languages include JavaScript Python Ruby PHP Lua and Perl The following are generally considered dynamic languages ActionScript BeanShell 2 C using Reflection Clojure CobolScript ColdFusion Markup Language Common Lisp and most other Lisps Dylan E Elixir Erlang FORTH Gambas GDScript Groovy 3 Java using Reflection JavaScript Julia Lua MATLAB Octave Objective C Perl PHP PowerShell Prolog Python R Raku Rebol Ruby Smalltalk SuperCollider Tcl VBScript Wolfram LanguageSee also editComparison of programming languages Name binding Von Neumann architectureReferences edit 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 Chapter 24 Dynamic language support Static springsource org Retrieved on 2013 07 17 lt Groovy Home Archived from the original on 2014 03 02 Retrieved 2014 03 02 Further reading editTratt Laurence 2009 Dynamically Typed Languages Advances in Computers Vol 77 pp 149 184 doi 10 1016 s0065 2458 09 01205 4 ISBN 9780123748126 External links edit Many use the term scripting languages Prechelt Lutz August 18 2002 Are Scripting Languages Any Good A Validation of Perl Python Rexx and Tcl against C C and Java PDF Advances in Computers 57 205 270 doi 10 1016 S0065 2458 03 57005 X ISBN 9780120121571 ISSN 0065 2458 Retrieved 2020 07 27 Bezroukov Nikolai 2013 A Slightly Skeptical View on Scripting Languages Softpanorama 2 1 ed Retrieved 2020 07 27 Wall Larry December 6 2007 Programming is Hard Let s Go Scripting Speech State of the Onion 11 Perl com Retrieved 2020 07 27 Roth Gregor November 20 2007 Scripting on the Java platform JavaWorld Retrieved 2020 07 27 Ousterhout John K March 1998 Scripting Higher Level Programming for the 21st Century PDF Computer Vol 31 no 3 pp 23 30 doi 10 1109 2 660187 ISSN 0018 9162 Archived from the original PDF on 2020 07 27 Retrieved 2020 07 27 ActiveState Announces Focus on Dynamic Languages ActiveState July 26 2004 Retrieved 2020 07 27 Ascher David July 27 2004 Dynamic Languages ready for the next challenges by design PDF Whitepapers ActiveState Archived from the original PDF on 2008 11 18 Ascher David July 27 2004 Dynamic Languages ready for the next challenges by design Whitepapers ActiveState Archived from the original on 2008 12 08 Retrieved from https en wikipedia org w index php title Dynamic programming language amp oldid 1216887512, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.