Dynamic programming language: Difference between revisions

Content deleted Content added
Undid revision 824270362 by Aofzaz Sex (talk)
 
(71 intermediate revisions by 59 users not shown)
Line 1:
{{Short description|Programming languages with runtime extensibility}}
{{about|a class of programming languages|the method for reducing the running time of algorithms|dynamic programming}}
{{Multiple issues|
{{expert-subject|date=January 2015}}
{{disputed|date=March 2012}}
{{confusing|date=October 2009}}
}}
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 program analysis|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.
'''Dynamic programming language''', in [[computer science]], is a class of [[high-level programming language]]s which, at [[Run time (program lifecycle phase)|runtime]], execute many common programming behaviors that static programming languages perform during [[compiler|compilation]]. These behaviors could include extension of the program, by adding new [[Source code|code]], by extending [[Object (computer science)|object]]s and definitions, or by modifying the [[type system]]. Although similar behaviours 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)|Lisp]] programming language.
 
Most dynamic languages are also [[dynamic typing|dynamically typed]], but not all are. Dynamic languages are frequently (but not always) referred to as "[[scripting language]]s", although the term "scripting language" in its narrowest sense refers to languages specific to a given run-time environment.
 
==Implementation==
Line 14 ⟶ 12:
 
===Eval===
Some dynamic languages offer an ''[[eval]]'' function. This function takes a string parameteror [[abstract syntax tree]] containing code in the language, and executes it. If this code stands for an expression, the resulting value is returned. However, [[Erik Meijer (computer scientist)|Erik Meijer]] and Peter Drayton suggestdistinguish thatthe programmers[[runtime "usecode generation]] offered by eval asfrom athe poor[[dynamic man'sloading]] substituteoffered forby [[shared libraries]] and warn that in many cases eval is used merely to implement [[higher-order function]]s (by passing functions as strings) or [[deserialization]]."<ref>{{Citation |url= | citeseerx = 10.1.1.69.5966 | title=Static Typing Where Possible, Dynamic Typing When Needed: The End of the Cold War Between Programming Languages | author=[[Erik Meijer (computer scientist)|Meijer, Erik]] and Peter Drayton | year=2005 | publisher=[[Microsoft]] Corporation |url=https://people.dsv.su.se/~beatrice/DYPL/meijer_drayton.pdf}}</ref>
 
===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]]s of existing types or objects. This can also refer to changing the [[Inheritance (computerobject-oriented scienceprogramming)|inheritance]] or type tree, and thus altering the way that existing types behave (especially with respect to the invocation of [[Method (computer science)|methods]]).
 
===FunctionalType programminginference===
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.
[[Functional programming]] concepts are a feature of many dynamic languages, and also derive from Lisp.
 
===Variable memory allocation===
====Closures====
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.
One of the most widely used aspects of functional programming in dynamic languages is the [[Closure (computer science)|closure]], which allows creating a new instance of a function which retains access to the context in which it was created. A simple example of this is generating a function for scanning text for a word:
 
'''function''' new_scanner (word)
temp_function = '''function''' (input)
scan_for_text (input, word)
'''end function'''
'''return''' temp_function
'''end function'''
 
Note that the inner function has no name, and is instead ''stored'' in the variable <code>temp_function</code>. Each time <code>new_scanner</code> is executed, it will return a new function which remembers the value of the <code>word</code> parameter that was passed in when it was defined.
 
Closures<ref>
See example of use on p.330 of [[Larry Wall]]'s ''[[Programming Perl]]'' {{ISBN|0-596-00027-8}}
</ref> are one of the core tools of functional programming, and many languages support at least this degree of functional programming.
 
====Continuations====
Another feature of some dynamic languages is the [[continuation]]. Continuations represent execution states that can be re-invoked. For example, a parser might return an intermediate result and a continuation that, when invoked, will continue to parse the input. Continuations interact in very complex ways with scoping, especially with respect to closures. For this reason, many dynamic languages do not provide continuations.
 
===Reflection===
[[Reflection (computer science)|Reflection]] is common in many dynamic languages, and typically involves [[Introspection (computer science)|analysis]] of the types and metadata of generic or [[Type polymorphism|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-expressionsexpression]]s.
 
===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 [[Macro (computer science)|macros]]. Most programmers today who are aware of the term ''macro'' have encountered them in [[C (programming language)|C]] or [[C++]], where they are a static feature which areis 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 language|Assembly]], [[C (programming language)|C]], [[C++]], early [[Java (programming language)|Java]], and [[FORTRANFortran]] do not generally fit into this category.{{clarify|date=September 2016}}
 
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.<ref>{{cite book| last=Harper| first=Robert | title=Practical Foundations for Programming languages | page=195 | year=2016 |publisher=Cambridge University Press| ___location=New York| isbn=9-781107-150300}}</ref>
[[Assembly language|Assembly]], [[C (programming language)|C]], [[C++]], early [[Java (programming language)|Java]], and [[FORTRAN]] do not generally fit into this category.{{clarify|date=September 2016}}
 
==Example code==
Line 55 ⟶ 39:
The example shows how a function can be modified at runtime from computed source code
 
<sourcesyntaxhighlight lang="lisp">
; the source code is stored as data in a variable
CL-USER > (defparameter *best-guess-formula* '(lambda (x) (* x x 2.5)))
Line 79 ⟶ 63:
CL-USER > (best-guess 10.3)
16.28573
</syntaxhighlight>
</source>
 
===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.
 
<sourcesyntaxhighlight lang="lisp">
; a person class. The person has a name.
CL-USER > (defclass person () ((name :initarg :name)))
Line 120 ⟶ 104:
CL-USER > *person-1*
#<PERSON Eva Luator age: 25>
</syntaxhighlight>
</source>
let foo = 42; // foo is now a number
foo = "bar"; // foo is now a string
foo = true; // foo is now a boolean
 
===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.
 
<sourcesyntaxhighlight lang="lisp">
; the class person
CL-USER > (defclass person () ((name :initarg :name)))
Line 176 ⟶ 163:
CL-USER 243 > *person-1*
#<PERSON Eva Luator ID: 42>
</syntaxhighlight>
</source>
 
==Examples==
Line 183 ⟶ 170:
* [[ActionScript]]
* [[BeanShell]]<ref>[http://static.springsource.org/spring/docs/2.0.x/reference/dynamic-language.html Chapter 24. Dynamic language support]. Static.springsource.org. Retrieved on 2013-07-17.</ref>
* [[C Sharp (programming language)|C#]] (using Reflectionreflection)]]
* Cobolscript
* [[Clojure]]
* [[CobolScript]]
* [[ColdFusion Markup Language]]
* [[Common Lisp]] and most other [[Lisp (programming language)|Lisps]]
* [[Dylan (programming language)|Dylan]]
* [[E programming language|E]]
* [[Elixir_Elixir (programming_languageprogramming language)|Elixir]]
* [[Erlang_Erlang (programming_languageprogramming language)|Erlang]]
* [[Forth (programming language)|Forth]]
* [[Gambas]]
* [[GDScript]]
* [[Groovy (programming language)|Groovy]]<ref>< {{cite web |url=http://groovy.codehaus.org/ |title=ArchivedGroovy copy- Home |accessdateaccess-date=2014-03-02 |deadurlurl-status=yesdead |archiveurlarchive-url=https://wwwweb.webcitationarchive.org/65Qmg4eQj?url=web/20140302111159/http://groovy.codehaus.org/ |archivedatearchive-date=20122014-0203-13 |df=02 }}</ref>
* [[Java (programming language)|Java (using Reflection)]]
* [[JavaScript]]
Line 200 ⟶ 189:
* [[MATLAB]] / [[GNU Octave|Octave]]
* [[Objective-C]]
* [[Object REXX|ooRexx]]
* [[Perl]]
* [[PHP]]
Line 206 ⟶ 196:
* [[Python (programming language)|Python]]
* [[R (programming language)|R]]
* [[Raku (programming language)|Raku]]
* [[Rebol]]
* [[Ring (programming language)|Ring]]
* [[Ruby (programming language)|Ruby]]
* [[Smalltalk]]
Line 213 ⟶ 205:
* [[VBScript]]
* [[Wolfram Language]]
* [[GDScript]]
 
==See also==
Line 224 ⟶ 215:
 
==Further reading==
* [http{{cite book|doi=10.1016/s0065-2458(09)01205-4|url=https://tratt.net/laurie/research/publicationspubs/html/tratt__dynamically_typed_languages/ Laurence Tratt, ''|title=Dynamically Typed Languages'', |volume=77|pages=149–184|series=Advances in Computers, vol. 77, pp. 149–184, July |year=2009]|last1=Tratt|first1=Laurence|isbn=9780123748126}}
 
==External links==
''(Many use the term "scripting languages".)''
* [http://page.mi.fu{{cite journal |last1=Prechelt |first1=Lutz |date=2002-berlin.de/~prechelt/Biblio/jccpprt2_advances2003.pdf08-18 |df=mdy |title=Are Scripting Languages Any Good? A Validation of Perl, Python, Rexx, and Tcl against C, C++, and Java |journal=Advances in Computers |volume=57 |pages=205–270 |issn=0065-2458 |doi=10.1016/S0065-2458(PDF03)]{{snd}}57005-X a|isbn=9780120121571 2003|url=https://page.mi.fu-berlin.de/prechelt/Biblio/jccpprt2_advances2003.pdf study|access-date=2020-07-27}}
* [{{cite web |last1=Bezroukov |first1=Nikolai |year=2013 |url=http://www.softpanorama.org/Articles/a_slightly_skeptical_view_on_scripting_languages.shtml |title=A Slightly Skeptical View on Scripting Languages] by Dr|edition=2.1 Nikolai|work=Softpanorama Bezroukov|access-date=2020-07-27}}
* [http{{cite speech |author-link1=Larry Wall |last1=Wall |first1=Larry |date=2007-12-06 |df=mdy |url=https://www.perl.com/pub/a/2007/12/06/soto-11.html/ |title=Programming is Hard, Let's Go Scripting...]{{snd}} |event=[[Perl.com transcript#State of [[Larry Wall]]'sthe Onion|State of the Onion]] speech11 |work=Perl.com |access-date=2020-07-27}}
* [http{{cite web |last1=Roth |first1=Gregor |date=2007-11-20 |df=mdy |url=https://www.javaworldinfoworld.com/javaworldarticle/jw2077792/scripting-11on-2007/jwthe-11java-jsr223platform.html |title=Scripting on the Java platform |work=[[JavaWorld]{{snd] |access-date=2020-07-27}} JavaWorld
* {{cite magazine |author-link1=John Ousterhout |last1=Ousterhout |first1=John K. |date=March 1998 |df=mdy |url=http://www.stanfordlibrary.us/~ouster/cgi-bin/papers/scripting.pdf |title=Scripting: Higher-Level Programming for the 21st Century |magazine=[[Computer (magazine)|Computer]] |volume=31 |issue=3 |pages=23–30 |issn=0018-9162 |doi=10.1109/2.660187 |access-date=2020-07-27 |archive-date=2020-07-27 |archive-url=https://web.archive.org/web/20200727185732/http://www.stanfordlibrary.us/~ouster/cgi-bin/papers/scripting.pdf |url-status=dead }}
 
* {{cite news |date=2004-07-26 |df=mdy |url=https://www.activestate.com/company/press/press-releases/activestate-announces-focus-dynamic-languages/ |title=ActiveState Announces Focus on Dynamic Languages |publisher=[[ActiveState]] |access-date=2020-07-27}}
<!-- AJA (2009-03-04) External link no longer active -->
** {{cite web |last1=Ascher |first1=David |date=2004-07-27 |df=mdy |url=https://www.activestate.com/Corporate/Publications/ActiveState_Dynamic_Languages.pdf |title=Dynamic Languages — ready for the next challenges, by design |department=Whitepapers |publisher=[[ActiveState]] |archive-url=https://web.archive.org/web/20081118035341/https://www.activestate.com/Corporate/Publications/ActiveState_Dynamic_Languages.pdf |archive-date=2008-11-18}}
 
** {{cite web |last1=Ascher |first1=David |date=2004-07-27 |df=mdy |url=http://www.activestate.com/company/newsroom/whitepapers_ADL.plex |title=Dynamic Languages — ready for the next challenges, by design |department=Whitepapers |publisher=[[ActiveState]] |archive-url=https://web.archive.org/web/20081208121835/http://www.activestate.com/company/newsroom/whitepapers_ADL.plex |archive-date=2008-12-08}}
{{Programming language}}
{{Types of programming languages}}
 
{{DEFAULTSORT:Dynamic Programming Language}}
[[Category:Evaluation strategy]]
[[Category:Dynamic programming languages| ]]