First-class object

From Wikipedia, the free encyclopedia

In computing, a first-class object (also -value, -entity, -citizen), in the context of a particular programming language, is an entity which can be used in programs without restriction (when compared to other kinds of objects in the same language). Depending on the language, this can imply:

  • being expressible as an anonymous literal value
  • being storable in variables
  • being storable in data structures
  • having an intrinsic identity (independent of any given name)
  • being comparable for equality with other entities
  • being passable as a parameter to a procedure/function
  • being returnable as the result of a procedure/function
  • being constructable at runtime

For example, in C and C++, it is not possible to create new functions at runtime (although see anonymous lambda bindings in the Boost library), whereas other kinds of object can be created at runtime. So functions in C are not first-class objects; sometimes they are called second-class objects because they can still be manipulated in most of the above fashions (via function pointers). Similarly, strings are not first class objects in FORTRAN 66 as it is not possible to assign them to variables, whereas numbers can be so assigned.

Smalltalk functions (methods) are first class objects, as are Smalltalk classes. Operators (+, -) are methods, so are also first class objects. In Smalltalk "everything is an object" but not quite everything is a first-class object. For instance, metaclasses are objects but do not have the flexibility of classes or other objects.

[edit] See also

In other languages