Jump to content

First-class citizen

From Wikipedia, the free encyclopedia

In a givenprogramming language design,afirst-class citizen[a]is an entity which supports all the operations generally available to other entities. These operations typically include being passed as anargument,returned from afunction,and assigned to avariable.[1]

History

[edit]

The concept of first- and second-class objects was introduced byChristopher Stracheyin the 1960s.[2][3]He did not actually define the term strictly, but contrasted real numbers and procedures inALGOL:

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)... [4]

Robin Popplestonegave the following definition: All items have certain fundamental rights.

  1. All items can be the actual parameters of functions
  2. All items can be returned as results of functions
  3. All items can be the subject of assignment statements
  4. All items can be tested for equality.[5]

During the 1990s,Raphael Finkel[6]proposed definitions of second and third class values, but these definitions have not been widely adopted.[7][better source needed]

Examples

[edit]

The simplestscalardata types, such as integer and floating-point numbers, are nearly always first-class.

In many older languages,arraysand strings are not first-class: they cannot be assigned as objects or passed as parameters to a subroutine. For example, neitherFortran IVnorCsupports 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 arraypointers,but in fact these are simply pointers to the array's first element, and again do not carry the array's size.[citation needed]

In most languages,data typesare not first-class objects, though in some object-oriented languages,classesare first-class objects and are instances ofmetaclasses.Languages in thefunctional programmingfamily often also feature first-class types, in the form of, for example,generalized algebraic data types,or other metalanguage amenities enabling programs to implement extensions to their own implementation language.

Few languages supportcontinuationsandGOTO-labels as objects at all, let alone as first-class objects.

Concept Description Languages
first-class function closuresandanonymous functions Smalltalk,Dart,Scheme,ML,Haskell,F#,Kotlin,Scala,Swift,Perl,PHP,Python,Raku,JavaScript,Delphi,Rust
first-class control continuations Scheme,ML,F#
first-class type dependent types Coq,Idris,Agda
first-class data type Generic Haskell,C++11
first-class polymorphism impredicative polymorphism
first-class message dynamicmessages(method calls) Smalltalk,[8]Objective-C,[8]Common Lisp
first-class class metaclassandmetaobject Smalltalk,Objective-C,Ruby,Python,Delphi,Common Lisp
first-class proofs proof object[9] Coq,Agda

Functions

[edit]

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 newfunctionsat runtime to call them 'first-class'.[citation needed]Under this definition, functions in C are not first-class objects; instead, they are sometimes calledsecond-class objects,because they can still be manipulated in most of the above fashions (viafunction pointers).

InSmalltalk,functions (methods) are first-class objects, just like Smalltalk classes. Since Smalltalk operators (+, -, etc.) are methods, they are also first-class objects.

Reflection

[edit]

Some languages, such asJavaandPHP,have an explicitreflectionsubsystem 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 theLispfamily, 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:

  • syntactic macrosorfexprs- 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 (reader macros);
  • ameta-circular evaluator- which provides a definition of the language'sevaluatoras a compiled tautologisation of itself, facilitating straightforward modification of the language without requiring ametalanguagedifferent from itself;
  • ametaobject protocol- a special form of meta-circular evaluator forobject-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

[edit]

Notes

[edit]
  1. ^Also known asfirst-class type,first-class object,first-class entity,orfirst-class value.

References

[edit]
  1. ^Scott, Michael (2006).Programming Language Pragmatics.San Francisco, CA: Morgan Kaufmann Publishers. p.140.ISBN9780126339512.
  2. ^Rod Burstall, "Christopher Strachey—Understanding Programming Languages",Higher-Order and Symbolic Computation13:52 (2000)
  3. ^Harold Abelson and Gerald Jay Sussman,Structure and Interpretation of Computer Programs,2nd edition, section 1.3.4footnote 64Archived2015-03-09 at theWayback Machine
  4. ^Christopher Strachey, "Fundamental Concepts in Programming Languages"inHigher-Order and Symbolic Computation13:11 (2000); though published in 2000, these are notes from lectures Strachey delivered in August, 1967
  5. ^R. J. Popplestone: The Design Philosophy ofPOP-2.in: D. Michie: Machine Intelligence 3, Edinburgh at the University Press, 1968
  6. ^Finkel, R.Advanced Programming language Design,p 73
  7. ^Norman Ramsey."About first-,second- and third-class value".stackoverflow.Retrieved14 September2013.
  8. ^abParitosh Shroff, Scott F. Smith.Type Inference for First-Class Messages with Match-Functions
  9. ^Bove, Ana; Dybjer, Peter (2009)."Dependent Types at Work"(PDF).Language Engineering and Rigorous Software Development.Lecture Notes in Computer Science. Vol. 5520. pp. 57–99.doi:10.1007/978-3-642-03153-3_2.ISBN978-3-642-03152-6.Archived(PDF)from the original on April 2, 2014.Retrieved8 June2015.(alsoarchived)