Could somebody clearly explain it with some good example. I came across this statement in Scala, when explaining about the functional programming.
In computer programming, a first-class object, first-class citizen, or first-class value is a language entity (e.g., function or variable) that operates as other entities in a language. For example, in the C programming language, you cannot pass a function to another function as a parameter.
A programming language is said to have First-class functions when functions in that language are treated like any other variable. For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable.
In Scala, functions are first-class citizens because we can do the following things with them: We can use functions as values or like normal variables; we can replace a variable or value with a function. We can assign a function literal to a variable. We can pass one or more functions as another function's parameters.
Functions in JavaScript are first-class objects (or “first-class citizens”).
Being "first-class" is not a formally defined notion, but it generally means that an entity has three properties:
It can be used, without restriction, wherever "ordinary" values can, i.e., passed and returned from functions, put in containers, etc.
It can be constructed, without restriction, wherever "ordinary" values can, i.e., locally, in an expression, etc.
It can be typed in a way similar to "ordinary" values, i.e., there is a type assigned to such an entity, and it can be freely composed with other types.
For functions, (2) particularly implies that a local function can use all names in scope, i.e. you have lexical closures. It also often comes with an anonymous form for construction (such as anonymous functions), but that is not strictly required (e.g. if the language has general enough let-expressions). Point (3) is trivially true in untyped languages.
So you see why functions in Scala (and in functional languages) are called first-class. Here are some other examples.
Functions in C/C++ are not first-class. While (1) and (3) are arguably available through function pointers, (2) is not supported for functions proper. (A point that's often overlooked.)
Likewise, arrays and structs are not first-class in C land.
Classes in Scala are not first-class. You can define and nest them, but not e.g. pass them to a function (only its instances). There are OO-languages with first-class classes, and in fact, the so-called nuObj calculus that informed Scala's design also allows that.
First-class modules are an often desired feature in ML-like languages. They are difficult, because they lead to undecidable type-checking. Some ML dialect allow modules to be wrapped up as first-class values, but arguably, that does not make modules first-class themselves.
It means that functions can be passed around the same way as integers, sequences, etc.
An example (although not Scala):
>>> def add2(x):
... return x + 2
...
>>> map(add2, [1, 2, 3])
[3, 4, 5]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With