first-class data type
   HOME

TheInfoList



OR:

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 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-orie ...
, 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 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 POP programming languages COWSEL (renamed POP-1), POP-2, POP-11, ...
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 retired 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 a ...
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
metaclass In object-oriented programming, a metaclass is a Class (computer science), class whose Instance (computer programming), instances are classes themselves. Unlike ordinary classes, which define the behaviors of objects, metaclasses specify the beha ...
es. 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 Coenzyme Q10 (CoQ10 ), also known as ubiquinone, is a naturally occurring biochemical cofactor (coenzyme) and an antioxidant produced by the human body. It can also be obtained from dietary sources, such as meat, fish, seed oils, vegetables, ...
, 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
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-orie ...
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 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 PHP is a general-purpose scripting language geared towards 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, mirror-like reflection of waves from a surface *** Mirror image, a reflection in a mirror or in water ** Diffuse r ...
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
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, interpreter, or even the parser ( reader macros); * 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 lambda ...
- 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 interp ...
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, quota ...
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 inc ...
- 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)