Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Metaprogramming
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Approaches == Metaprogramming enables developers to write programs and develop code that falls under the [[generic programming]] [[paradigm]]. Having the programming language itself as a [[First-class object|first-class data type]] (as in [[Lisp (programming language)|Lisp]], [[Prolog]], [[SNOBOL]], or [[Rebol]]) is also very useful; this is known as ''[[homoiconicity]]''. [[Generic programming]] invokes a metaprogramming facility within a language by allowing one to write code without the concern of specifying data types since they can be supplied as [[parameter (computer programming)|parameters]] when used. Metaprogramming usually works in one of three ways.<ref>{{cite web|last1=Joshi|first1=Prateek|title=What Is Metaprogramming? β Part 2/2|url=https://prateekvjoshi.com/2014/04/05/what-is-metaprogramming-part-22/|website=Perpetual Enigma|date=5 April 2014|access-date=14 August 2014}}</ref> # The first approach is to expose the internals of the [[runtime system]] (engine) to the programming code through [[application programming interface]]s (APIs) like that for the [[.NET]] [[Common Intermediate Language]] (CIL) emitter. # The second approach is [[Out-of-order execution|dynamic execution]] of expressions that contain programming commands, often composed from strings, but can also be from other methods using arguments or context, like [[JavaScript]].<ref>for example, instance_eval in [[Ruby (programming language)|Ruby]] takes a string or an anonymous function. {{cite web |url=https://docs.ruby-lang.org/en/master/BasicObject.html#method-i-instance_eval |title=Class BasicObject Ruby 3.5}}</ref> Thus, "programs can write programs." Although both approaches can be used in the same language, most languages tend to lean toward one or the other. # The third approach is to step outside the language entirely. General purpose [[program transformation]] systems such as [[compiler]]s, which accept language descriptions and carry out arbitrary transformations on those languages, are direct implementations of general metaprogramming. This allows metaprogramming to be applied to virtually any target language without regard to whether that target language has any metaprogramming abilities of its own. One can see this at work with [[Scheme (programming language)|Scheme]] and how it allows tackling some limits faced in [[C (programming language)|C]] by using constructs that are part of the Scheme language to extend C.<ref>{{cite web|title=Art of Metaprogramming|website=[[IBM]] |url=http://www.ibm.com/developerworks/library/l-metaprog1/}}</ref> [[Lisp (programming language)|Lisp]] is probably the quintessential language with metaprogramming facilities, both because of its historical precedence and because of the simplicity and power of its metaprogramming. In Lisp metaprogramming, the unquote operator (typically a comma) introduces code that is [[Lisp (programming language)#Self-evaluating forms and quoting|evaluated at program definition time]] rather than at run time. The metaprogramming language is thus identical to the host programming language, and existing Lisp routines can be directly reused for metaprogramming if desired. This approach has been implemented in other languages by incorporating an interpreter in the program, which works directly with the program's data. There are implementations of this kind for some common high-level languages, such as [[RemObjects]]β [[Pascal Script]] for [[Object Pascal]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)