First-class object
Encyclopedia
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. In computer science the term reification
is used when referring to the process (technique, mechanism) of making something a first-class object.
The term was coined by Christopher Strachey
in the context of “functions as first-class citizens” in the mid-1960s.
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.
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 pointer
s). 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 continuation
s and GOTO
-labels as first-class objects, though arguably they don't support them as objects at all.
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
, is an entity that can be constructed at run-time, passed as a parameter, returned from a subroutine, or assigned into a variable. In computer science the term reification
Reification (computer science)
Reification is the process by which an abstract idea about a computer program is turned into an explicit data model or other object created in a programming language. A computable/addressable object — a resource — is created in a system as a proxy for a non computable/addressable object...
is used when referring to the process (technique, mechanism) of making something a first-class object.
The term was coined by Christopher Strachey
Christopher Strachey
Christopher Strachey was a British computer scientist. He was one of the founders of denotational semantics, and a pioneer in programming language design...
in the context of “functions as first-class citizens” in the mid-1960s.
Definition
An object is first-class when it:- can be stored in variables and data structures
- can be passed as a parameter to a subroutine
- can be returned as the result of a subroutine
- can be constructed at run-time
- has intrinsic identity (independent of any given name)
The term "object" is used loosely here, not necessarily referring to objects in object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
. The simplest scalar
Scalar (computing)
In computing, a scalar variable or field is one that can hold only one value at a time; as opposed to composite variables like array, list, hash, record, etc. In some contexts, a scalar value may be understood to be numeric. A scalar data type is the type of a scalar variable...
data types, such as integer and floating-point numbers, are nearly always first-class.
Examples
In CC (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
, 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 pointer
Function pointer
A function pointer is a type of pointer in C, C++, D, and other C-like programming languages, and Fortran 2003. When dereferenced, a function pointer can be used to invoke a function and pass it arguments just like a normal function...
s). Similarly, strings are not first class objects in FORTRAN 66
Fortran
Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...
because it is not possible to assign them to variables (unlike, for example, numbers).
In Smalltalk
Smalltalk
Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...
, 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
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
) 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 continuation
Continuation
In computer science and programming, a continuation is an abstract representation of the control state of a computer program. A continuation reifies the program control state, i.e...
s and GOTO
Goto
goto is a statement found in many computer programming languages. It is a combination of the English words go and to. It performs a one-way transfer of control to another line of code; in contrast a function call normally returns control...
-labels as first-class objects, though arguably they don't support them as objects at all.
Concept | Description | Languages |
---|---|---|
first-class function First-class function In computer science, a programming language is said to have first-class functions if it treats functions as first-class objects. Specifically, this means that the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning... |
Scheme, ML, Haskell Haskell (programming language) Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the... |
|
first-class control | continuation Continuation In computer science and programming, a continuation is an abstract representation of the control state of a computer program. A continuation reifies the program control state, i.e... s |
Scheme, ML |
first-class type | Coq Coq In computer science, Coq is an interactive theorem prover. It allows the expression of mathematical assertions, mechanically checks proofs of these assertions, helps to find formal proofs, and extracts a certified program from the constructive proof of its formal specification... |
|
first-class data type | Generic Haskell | |
first-class polymorphism | impredicative polymorphism |
Second and third class objects
Rafael Finkel 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 |