First-Class Objects in Programming
Definition
In programming, a First Class Object is an element that satisfies the following conditions:
- (i) Can be a real argument of a function.
- (ii) Can be a return value of a function.
- (iii) Can be the target of an assignment statement.
- (iv) Can be the subject of equality comparison.
Example
Simply put, something that can be treated like a regular number is called a first class object, precisely because a ‘regular number’ is indeed a first class object. Taking the integer $3$ as an example,
- (i) It can be a parameter for some function $f$ as in $f(3)$,
- (ii) It can be returned by a function that returns the $n$th Fibonacci number $\text{fibo}$ as in $\text{fibo}(4) = 3$,
- (iii) It can be assigned as in $x = 3$, and
- (iv) It can be compared for equality as in $3==4$.
For the Mathematician
The concept of a first class object is primarily mentioned in the context of explaining functional languages. Mathematically, the idea of a first class object itself may seem odd, as almost every object should naturally be a first class object according to the mathematical definition of a function. Functions themselves are elements of a set, so it should be entirely possible to define a function that takes a function as a variable, and whose value is a function, and to define, or compare functions.
However, this is strictly speaking within the realm of mathematics. While perspectives may vary, programming is generally considered part of computer science, thus, a different domain. To readily accept such explanations, one must be sufficiently familiar with mathematics and abstract logic1.
In fact, even learners who have just completed the high school curriculum may not naturally view functions as objects. To mathematicians, a function $f(x) = x^2 + 1$ might be seen as an element of the set of quadratic functions $f \in p_{2}(x)$ or as an element of the set of continuous functions $f \in C(\mathbb{R})$, but if one only has the ability to abstract at the level of the curriculum, it may be sufficient to conceive it as returning $x^2+1$ when $x$ is entered.
For the Engineer
Adding functions together—having a function return from adding functions—may come naturally if you understand vector spaces, but it can still be sufficiently grasped without going that deep. However, the idea of a function itself being an input can be more challenging to visualize. Let’s define the following function $I$ anew. $$ I(f) := \int_{0}^{6} f(x) dx $$ The function $I$ is a function that takes a function as an argument and calculates the definite integral from $0$ to $6$. Thus, the ‘return’ depending on the ‘input’ function would be calculated as follows. $$ f(x) = x^2+1 \implies I(f) = 78 \\ g(x) = x-1 \implies I(g) = 12 $$ In this way, if the function itself is a first class object, it can be said to have one of the qualities of functional languages. This does not solely make a language functional, but the trend is for languages, even imperative ones, to increasingly accept this concept. Roughly speaking, they can be referred to as ‘also being functional languages’ without much problem in conveying the meaning.
In fact, writings that explain the concept of first class objects from a programmer’s perspective often specify concrete examples with certain languages. ↩︎