ArticleslgStudy

computer science

First-class citizen

First-class citizen is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand First-class citizen rather than just read about it. In short: In a given programming language design, 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, returned from a function, and assigned to a variable.

Key takeaways

  • First-class citizen belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect First-class citizen to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of First-class citizen from memory before moving on to harder problems.

Reference excerpt

In a given programming language design, 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, returned from a function, and assigned to a variable.

History The concept of first- and second-class objects was introduced by Christopher Strachey in the 1960s. 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 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 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 proposed definitions of second and third class values, but these definitions have not been widely adopted.

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 are instances of metaclasses. Languages in the functional programming family often also feature first-class types, in the form of, for example, generalized algebraic data types, or other metalanguage amenities enabling programs to implement extensions to their own implementation language. Few languages support continuations and GOTO-labels as objects at all, let alone as first-class objects.

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 functions 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 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

Some languages, such as Java and PHP, have an explicit reflection 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 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 fexprs – 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 – which provides a definition of the language's evaluator as a compiled tautologisation of itself, facilitating straightforward modification of the language without requiring a metalanguage different from itself; a metaobject protocol – a special form of meta-circular evaluator for object-oriented programming, 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 Reification

Notes

References

Worked examples

Example 1 — a first encounter with First-class citizen

Start with the simplest possible case. Write down what First-class citizen claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to First-class citizen before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about First-class citizen ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of First-class citizen

In research
First-class citizen appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses First-class citizen in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
First-class citizen is common in secondary-school and first-year university syllabi. It links to neighbouring topics Object (computer science), Programming constructs, Programming language design, so understanding it makes those chapters shorter.
In everyday life
Look for First-class citizen outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “First-class citizen” →

How to study First-class citizen in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what First-class citizen means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain First-class citizen out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is First-class citizen in simple terms?

In a given programming language design, 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, returned from a function, and assigned to a variable.

Why does First-class citizen matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study First-class citizen?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on First-class citizen.

Tags

  • Object (computer science)
  • Programming constructs
  • Programming language design

Keep exploring