In a given
programming language design
Programming languages are typically created by designing a form of representation of a computer program, and writing an implementation for the developed concept, usually an interpreter or compiler. Interpreters are designed to read programs, usu ...
, a first-class citizen is an entity which supports all the operations generally available to other entities. These operations typically include being passed as an
argument
An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persu ...
, returned from a
function, and assigned to a
variable.
History
The concept of first- and second-class objects was introduced by
Christopher Strachey
Christopher S. Strachey (; 16 November 1916 – 18 May 1975) was a British computer scientist. He was one of the founders of denotational semantics, and a pioneer in programming language design and computer time-sharing.F. J. Corbató, et al., T ...
in the 1960s. He did not actually define the term strictly, but contrasted real numbers and procedures in
ALGOL
ALGOL (; short for "Algorithmic Language") is a family of imperative computer programming languages originally developed in 1958. ALGOL heavily influenced many other languages and was the standard method for algorithm description used by the ...
:
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)...
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.
During the 1990s,
Raphael Finkel proposed definitions of second and third class values, but these definitions have not been widely adopted.
Examples
The simplest
scalar data types, such as integer and floating-point numbers, are nearly always first-class.
In many older languages,
arrays
An array is a systematic arrangement of similar objects, usually in rows and columns.
Things called an array include:
{{TOC right
Music
* In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
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 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
pointers
Pointer may refer to:
People with the name
* Pointer (surname), a surname (including a list of people with the name)
* Pointer Williams (born 1974), American former basketball player
Arts, entertainment, and media
* ''Pointer'' (journal), the ...
, but in fact these are simply pointers to the array's first element, and again do not carry the array's size.
In most languages,
data types
In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
are not first-class objects, though in some object-oriented languages,
classes are first-class objects and are instances of
metaclasses. Languages in the
functional programming
In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declarat ...
family often also feature first-class types, in the form of, for example,
generalized algebraic data type
In functional programming, a generalized algebraic data type (GADT, also first-class phantom type, guarded recursive datatype, or equality-qualified type) is a generalization of a Parametric polymorphism, parametric algebraic data type (ADT).
Ove ...
s, or other metalanguage amenities enabling programs to implement extensions to their own implementation language.
Few languages support
continuation
In computer science, a continuation is an abstract representation of the control state of a computer program. A continuation implements ( reifies) the program control state, i.e. the continuation is a data structure that represents the computat ...
s and
GOTO-labels as objects at all, let alone as first-class objects.
} --> (als
archived , ,
Coq,
Agda
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
functions at runtime to call them 'first-class'. 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
A function pointer, also called a subroutine pointer or procedure pointer, is a pointer referencing executable code, rather than data. Dereferencing the function pointer yields the referenced function, which can be invoked and passed arguments ...
s).
In
Smalltalk
Smalltalk is a purely object oriented programming language (OOP) that was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learni ...
, functions (methods) are first-class objects, just like Smalltalk classes. Since Smalltalk operators (+, -, etc.) are methods, they are also first-class objects.
Reflection
Some languages, such as
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
and
PHP, have an explicit
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
Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation.
Originally specified in the late 1950s, ...
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:
*
syntactic macros or
fexprs - 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);
* a
meta-circular evaluator - which provides a definition of the language's
evaluator as a compiled tautologisation of itself, facilitating straightforward modification of the language without requiring a
metalanguage different from itself;
* a
metaobject protocol - a special form of meta-circular evaluator for
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
, 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
In computer science, a programming language is said to have first-class functions if it treats function (programming), functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning ...
*
Reification
Notes
References
{{DEFAULTSORT:First-Class Object
Programming constructs
Programming language design
Object (computer science)