First-class citizen

In programming language design, a first-class citizen (also object, entity, or value), in the context of a particular programming language, is an entity that can be constructed at run-time, passed as a parameter, returned from a subroutine, or assigned into a variable.[1] In computer science the term reification is used when referring to the process (technique, mechanism) of making something a first-class object.[2]

The term was coined by Christopher Strachey in the context of “functions as first-class citizens” in the mid-1960s.[3]

Contents

Definition

An object is first-class when it:[4][5]

The term "object" is used loosely here, not necessarily referring to objects in object-oriented programming. The simplest scalar data types, such as integer and floating-point numbers, are nearly always first-class.

Examples

In C and C++, it is not possible to create new functions at runtime, whereas other kinds of objects can be created at runtime. As a result, 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 pointers). Similarly, strings are not first class objects in FORTRAN 66 because it is not possible to assign them to variables (unlike, for example, numbers).

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

In many older languages (for example C) arrays were not first-class: they could not be assigned as objects or passed as a parameter to a subroutine; only their elements could be directly manipulated. Few languages support continuations and GOTO-labels as first-class objects, though arguably they don't support them as objects at all.

Concept Description Languages
first-class function Scheme, ML, Haskell
first-class control continuations Scheme, ML
first-class type Coq
first-class data type Generic Haskell
first-class polymorphism impredicative polymorphism

Second and third class objects

Rafael Finkel [6]offers definitions of second and third class objects (or, as he calls them, values). However, his definition of "first class object" is contrary to other authors': specifically, he does not require run-time constructability, so he would consider C functions to be first-class.

Manipulation First Second Third
Pass value as a parameter yes yes no
Return value from a procedure yes no no
Assign value into a variable yes no no

See also

References

  1. ^ Scott, Michael (2006). Programming Language Pragmatics. San Francisco, CA: Morgan Kaufmann Publishers. p. 140. 
  2. ^ J. Malenfant, M. Jacques and F.-N. Demers. "A Tutorial on Behavioral Reflection and its Implementation". parc.com. http://www2.parc.com/csl/groups/sda/projects/reflection96/docs/malenfant/ref96/ref96.html. Retrieved 2010-10-09. 
  3. ^ Rod Burstall, "Christopher Strachey—Understanding Programming Languages", Higher-Order and Symbolic Computation 13:52 (2000)
  4. ^ "First Class". C2.com. 2006-01-25. http://www.c2.com/cgi/wiki?FirstClass. Retrieved 2010-10-09. 
  5. ^ "first class object". Catalysoft.com. http://www.catalysoft.com/definitions/firstClassObject.html. Retrieved 2010-10-09. 
  6. ^ Finkel, R. Advanced Programming language Design, p 73