In
programming language design
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming la ...
, a first-class citizen (also type, object, entity, or value) in a given
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
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 statement or group of statements called premises intended to determine the degree of truth or acceptability of another statement called conclusion. Arguments can be studied from three main perspectives: the logical, the dialectic ...
, returned from a
function
Function or functionality may refer to:
Computing
* Function key, a type of key on computer keyboards
* Function model, a structured representation of processes in a system
* Function object or functor or functionoid, a concept of object-oriente ...
, and assigned to a
variable
Variable may refer to:
* Variable (computer science), a symbolic name associated with a value and whose associated value may be changed
* Variable (mathematics), a symbol that represents a quantity in a mathematical expression, as used in many ...
.
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., ...
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
Robin John Popplestone (9 December 1938 in Bristol – 14 April 2004 in Glasgow) was a pioneer in the fields of machine intelligence and robotics. He is known for developing the COWSEL and POP-2, POP programming languages, and for his work on Fr ...
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
Raphael Finkel (born 1951) is an American computer scientist and a professor at the University of Kentucky. He compiled the first version of the Jargon File. He is the author of ''An Operating Systems Vade Mecum'',[scalar
Scalar may refer to:
*Scalar (mathematics), an element of a field, which is used to define a vector space, usually the field of real numbers
* Scalar (physics), a physical quantity that can be described by a single element of a number field such ...]
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:
Places
* Pointer, Kentucky
* Pointers, New Jersey
* Pointers Airport, Wasco County, Oregon, United States
* The Pointers, a pair of rocks off Antarctica
People with the name
* Pointer (surname), a surname (including a l ...
, 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 set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
are not first-class objects, though in some object-oriented languages,
classes are first-class objects and are instances of
metaclass
In object-oriented programming, a metaclass is a class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances. Not all object-oriente ...
es.
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 computati ...
s and
GOTO
GoTo (goto, GOTO, GO TO or other case combinations, depending on the programming language) is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function ca ...
-labels as objects at all, let alone as first-class objects.
} --> (als
archived , ,
Coq
Coq is an interactive theorem prover first released in 1989. It allows for expressing mathematical assertions, mechanically checks proofs of these assertions, helps find formal proofs, and extracts a certified program from the constructive proof ...
,
Agda Agda may refer to:
* Agda (programming language), the programming language and theorem prover
* Agda (Golgafrinchan), the character in ''The Hitchhiker's Guide to the Galaxy'' by Douglas Adams
* Liten Agda, the heroine of a Swedish legend
* Agda M ...
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
Function or functionality may refer to:
Computing
* Function key, a type of key on computer keyboards
* Function model, a structured representation of processes in a system
* Function object or functor or functionoid, a concept of object-oriente ...
s 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 that points to a function. As opposed to referencing a data value, a function pointer points to executable code within memory. Dereferencing the function poin ...
s).
In
Smalltalk
Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan Ka ...
, 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 (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
and
PHP
PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group ...
, have an explicit
reflection Reflection or reflexion may refer to:
Science and technology
* Reflection (physics), a common wave phenomenon
** Specular reflection, reflection from a smooth surface
*** Mirror image, a reflection in a mirror or in water
** Signal reflection, in ...
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
A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech.
Types
* A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lisping ...
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
fexpr In Lisp programming languages, a fexpr is a function whose operands are passed to it without being evaluated. When a fexpr is called, only the body of the fexpr is evaluated; no other evaluations take place except when explicitly initiated by the fe ...
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 or interpreter;
* a
meta-circular evaluator In computing, a meta-circular evaluator (MCE) or meta-circular interpreter (MCI) is an interpreter which defines each feature of the interpreted language using a similar facility of the interpreter's host language. For example, interpreting a lambd ...
- which provides a definition of the language's
evaluator
In computer science, an interpreter is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program. An interprete ...
as a compiled tautologisation of itself, facilitating straightforward modification of the language without requiring a
metalanguage
In logic and linguistics, a metalanguage is a language used to describe another language, often called the ''object language''. Expressions in a metalanguage are often distinguished from those in the object language by the use of italics, quot ...
different from itself;
* a
metaobject protocol In computer science, a metaobject is an object that manipulates, creates, describes, or implements objects (including itself). The object that the metaobject pertains to is called the base object. Some information that a metaobject might define incl ...
- 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", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
, in which the object system implements itself recursively via a system of metaclasses and metaobjects.
These allow varying forms of first-class access to the language implementation, and are generally 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 functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from ...
*
Reification
References
{{DEFAULTSORT:First-Class Object
Programming constructs
Programming language design
Object (computer science)