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
First-class citizen
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!
{{Short description|Concept in programming language design}} {{for|the usage in society|Second-class citizen}} In a given [[Programming language#Design and implementation|programming language design]], a '''first-class citizen'''{{efn|Also known as '''first-class type''', '''first-class object''', '''first-class entity''', or '''first-class value'''.}} is an entity which supports all the operations generally available to other entities. These operations typically include being passed as an [[Parameter (computer programming)|argument]], returned from a [[Function (computer programming)|function]], and assigned to a [[Variable (computer science)|variable]].<ref>{{cite book |title= Programming Language Pragmatics |last= Scott |first= Michael |year= 2006 |publisher= Morgan Kaufmann Publishers |location= San Francisco, CA |page= [https://archive.org/details/programminglangu00scot_912/page/n172 140] |isbn= 9780126339512 |url=https://archive.org/details/programminglangu00scot_912|url-access= limited }}</ref> ==History== The concept of first- and second-class objects was introduced by [[Christopher Strachey]] in the 1960s.<ref>Rod Burstall, "Christopher Strachey—Understanding Programming Languages", ''Higher-Order and Symbolic Computation'' '''13''':52 (2000)</ref><ref>Harold Abelson and Gerald Jay Sussman, ''Structure and Interpretation of Computer Programs'', 2nd edition, section 1.3.4 [http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#footnote_Temp_121 footnote 64] {{Webarchive|url=https://web.archive.org/web/20150309062836/http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#footnote_Temp_121 |date=2015-03-09 }}</ref> He did not actually define the term strictly, but contrasted real numbers and procedures in [[ALGOL]]: <blockquote>'''First and second class objects.''' In ALGOL, a real number may appear in an expression or be assigned to a variable, and either of them may appear as an actual parameter in a procedure call. A procedure, on the other hand, may only appear in another procedure call either as the operator (the most common case) or as one of the actual parameters. There are no other expressions involving procedures or whose results are procedures. Thus in a sense procedures in ALGOL are second class citizens—they always have to appear in person and can never be represented by a variable or expression (except in the case of a formal parameter)... <ref>Christopher Strachey, "[http://www.ics.uci.edu/~jajones/INF102-S18/readings/05_stratchey_1967.pdf Fundamental Concepts in Programming Languages]" in ''Higher-Order and Symbolic Computation'' '''13''':11 (2000); though published in 2000, these are notes from lectures Strachey delivered in August, 1967</ref></blockquote> [[Robin Popplestone]] gave the following definition: All items have certain fundamental rights. # All items can be the actual parameters of functions # All items can be returned as results of functions # All items can be the subject of assignment statements # All items can be tested for equality.<ref>R. J. Popplestone: The Design Philosophy of [[POP-2]]. in: D. Michie: Machine Intelligence 3, Edinburgh at the University Press, 1968</ref> During the 1990s, [[Raphael Finkel]]<ref>Finkel, R. ''Advanced Programming language Design'', p 73</ref> proposed definitions of second and third class values, but these definitions have not been widely adopted.<ref>{{cite web|title=About first-,second- and third-class value|author=Norman Ramsey|url=https://stackoverflow.com/a/2582804|work=stackoverflow.com|access-date=14 September 2013}}</ref>{{better source needed|date=November 2021}} ==Examples== The simplest [[Scalar (computing)|scalar]] data types, such as integer and floating-point numbers, are nearly always first-class. In many older languages, [[array data type|arrays]] and strings are not first-class: they cannot be assigned as objects or passed as parameters to a subroutine. For example, neither [[Fortran IV]] nor [[C (programming language)|C]] supports array assignment, and when they are passed as parameters, only the position of their first element is actually passed—their size is lost. C appears to support assignment of array [[pointer (computing)|pointers]], but in fact these are simply pointers to the array's first element, and again do not carry the array's size.{{citation needed|date=January 2022}} In most languages, [[data types]] are not first-class objects, though in some object-oriented languages, [[Class (computer programming)|classes]] are first-class objects and are instances of [[metaclass]]es. Languages in the [[functional programming]] family often also feature first-class types, in the form of, for example, [[generalized algebraic data type]]s, or other metalanguage amenities enabling programs to implement extensions to their own implementation language. Few languages support [[continuation]]s and [[GOTO]]-labels as objects at all, let alone as first-class objects. {| class=wikitable ! Concept !! Description !! Languages |- | [[first-class function]] || [[closure (computer science)|closure]]s and [[anonymous function]]s || [[Smalltalk]], [[Dart (programming language)|Dart]], [[Scheme (programming language)|Scheme]], [[ML (programming language)|ML]], [[Haskell (programming language)|Haskell]], [[F Sharp (programming language)|F#]], [[Kotlin (programming language)|Kotlin]], [[Scala (programming language)|Scala]], [[Swift (programming language)|Swift]], [[Perl]], [[PHP]], [[Python (programming language)|Python]], [[Raku (programming language)|Raku]], [[JavaScript]], [[Delphi (programming language)|Delphi]], [[Rust (programming language)|Rust]], [[Common Lisp#Functions|Common Lisp]] |- | [[first-class control]] || [[continuation]]s || [[Scheme (programming language)|Scheme]], [[ML (programming language)|ML]], [[F Sharp (programming language)|F#]] |- | [[first-class type]] || [[dependent types]] || [[Coq (software)|Coq]], [[Idris (programming language)|Idris]], [[Agda (programming language)|Agda]] |- | [[first-class data type]] || || [[Generic Haskell]], C++11 |- | [[first-class polymorphism]] || [[impredicative polymorphism]] || |- | [[first-class message]] || dynamic [[message (object-oriented programming)|message]]s (method calls) || [[Smalltalk]],<ref name="shroff-smith">Paritosh Shroff, Scott F. Smith. ''[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.114.4468&rep=rep1&type=pdf Type Inference for First-Class Messages with Match-Functions]''</ref> [[Objective-C]],<ref name="shroff-smith" /> [[Generic_function#In_Common_Lisp_Object_System|Common Lisp]] |- | [[first-class class]] || [[metaclass]] and [[metaobject]] || [[Smalltalk]], [[Objective-C]], [[Ruby (programming language)|Ruby]], [[Python (programming language)|Python]], [[Delphi (programming language)|Delphi]], [[Common Lisp#Common_Lisp_Object_System_(CLOS)|Common Lisp]] |- | first-class proofs <!-- proofs are first-class in IIT according to "Dependent Types at Work" --> || [[proof object]]<ref>{{cite book|last2=Dybjer|first2=Peter|date=2009|chapter=Dependent Types at Work|chapter-url=http://www.cse.chalmers.se/~peterd/papers/DependentTypesAtWork.pdf|title=Language Engineering and Rigorous Software Development|volume=5520|pages=57–99|doi=10.1007/978-3-642-03153-3_2|last1=Bove|first1=Ana|series=Lecture Notes in Computer Science |isbn=978-3-642-03152-6 |access-date=8 June 2015|url-status=live|archive-url=https://web.archive.org/web/20140402095457/http://www.cse.chalmers.se/~peterd/papers/DependentTypesAtWork.pdf|archive-date=April 2, 2014}} <!-- add back if original becomes obsolete | archiveurl = https://web.archive.org/web/20140402095457/http://www.cse.chalmers.se/~peterd/papers/DependentTypesAtWork.pdf | archivedate = 2014-04-02}} --> (also [http://www.cse.chalmers.se/~peterd/papers/DependentTypesAtWork.pdf archived])</ref> || [[Coq (software)|Coq]], [[Agda (programming language)|Agda]] |} ==Functions== {{main article|First-class functions}} Many programming languages support passing and returning function values, which can be applied to arguments. Whether this suffices to call function values first-class is disputed. Some authors require it be possible to create new [[function (programming)|function]]s at runtime to call them 'first-class'.{{citation needed|date=January 2022}} Under this definition, functions in C are not first-class objects; instead, they are sometimes called '''second-class objects''', because they can still be manipulated in most of the above fashions (via [[function pointer]]s). In [[Smalltalk]], functions (methods) are first-class objects, just like Smalltalk classes. Since Smalltalk operators (+, -, etc.) are methods, they are also first-class objects. ==Reflection== {{main article|Reflection (computer programming)}} Some languages, such as [[Java programming language|Java]] and [[PHP]], have an explicit [[Reflection (computer programming)|reflection]] subsystem which allow access to internal implementation structures even though they are not accessible or manipulable in the same way as ordinary objects. In other languages, such as those in the [[Lisp (programming language)|Lisp]] family, reflection is a central feature of the language, rather than a special subsystem. Typically this takes the form of some set of the following features: * [[Macro (computer science)#Syntactic_macros|syntactic macros]] or [[fexpr]]s - which allow the user to write code which handles code as data and evaluates it by discretion, enabling, for example, programs to write programs (or rewrite themselves) inside of the compiler, interpreter, or even the parser ([[Lisp reader|reader macro]]s); * a [[meta-circular evaluator]] - which provides a definition of the language's [[eval|evaluator]] as a compiled tautologisation of itself, facilitating straightforward modification of the language without requiring a [[metalanguage]] different from itself; * a [[Metaobject#Metaobject_protocol|metaobject protocol]] - a special form of meta-circular evaluator for [[object-oriented programming]], in which the object system implements itself recursively via a system of metaclasses and metaobjects, which are themselves classes and objects. These allow varying forms of first-class access to the language implementation, and are, in general, manipulable in the same way as, and fully indistinguishable from, ordinary language objects. Because of this, their usage generally comes with some (cultural) stipulations and advice, as untested modification of the core programming system by users can easily undermine performance optimisations made by language implementers. == See also == * [[First-class function]] * [[Reification (computer science)|Reification]] ==Notes== {{notelist}} ==References== {{reflist}} {{DEFAULTSORT:First-Class Object}} <!--Categories--> [[Category:Programming constructs]] [[Category:Programming language design]] [[Category:Object (computer science)]]
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)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Better source needed
(
edit
)
Template:Citation needed
(
edit
)
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Efn
(
edit
)
Template:For
(
edit
)
Template:Main article
(
edit
)
Template:Notelist
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Webarchive
(
edit
)