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 |
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.
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 |
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 |