First-class citizen

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

In programming language design, a first-class citizen (also type, object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, and assigned to a variable.[1]

History

The concept of first- and second- class objects was introduced by Christopher Strachey in the 1960s.[2][3] He did not actually define the term strictly, but contrasted real numbers and procedures in ALGOL:

First and second class objects. In Algol, a real number may appear in an expression or be assigned to a variable, and either 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)... [4]

During the 1990s, Raphael Finkel[5] proposed definitions of second and third class values, but these definitions have not been widely adopted.[6]

Examples

The simplest scalar data types, such as integer and floating-point numbers, are nearly always first-class.

In many older languages, arrays 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, 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 are not first-class objects, though in some object-oriented languages classes are first-class objects, and used for metaclasses.

Few languages support continuations and GOTO-labels as objects at all, let alone as first-class objects.

Concept Description Languages
first-class function closures Scheme, ML, Haskell, F#, Scala, Swift
first-class control continuations Scheme, ML, F#
first-class type Coq
first-class data type Generic Haskell
first-class polymorphism impredicative polymorphism
first-class message dynamic messages (method calls) Smalltalk,[7] Objective-C[7]
first-class class metaclass Smalltalk, Objective-C, Ruby, Python
proof object[8] Coq, Agda

Functions

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

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

In Smalltalk, functions (methods) are first-class objects, just like Smalltalk classes. Since Smalltalk operators (+, -, etc.) are methods, they are also first-class objects.

Reflection

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

Some languages, such as Java, have an explicit reflection subsystems which allow access to internal implementation structures even though they are not accessible or manipulable in the same way as ordinary objects.

See also

References

  1. Lua error in package.lua at line 80: module 'strict' not found.
  2. Rod Burstall, "Christopher Strachey—Understanding Programming Languages", Higher-Order and Symbolic Computation 13:52 (2000)
  3. Harold Abelson and Gerald Jay Sussman, Structure and Interpretation of Computer Programs, 2nd edition, section 1.3.4 footnote 64
  4. Christopher Strachey, "Fundamental Concepts in Programming Languages" in Higher-Order and Symbolic Computation 13:11 (2000); though published in 2000, these are notes from lectures Strachey delivered in August, 1967
  5. Finkel, R. Advanced Programming language Design, p 73
  6. Lua error in package.lua at line 80: module 'strict' not found.
  7. 7.0 7.1 Paritosh Shroff, Scott F. Smith. Type Inference for First-Class Messages with Match-Functions
  8. Lua error in package.lua at line 80: module 'strict' not found. (also archived)