I have seen many times statements like- "Please make this feature a first class citizen in so and so language/platform". For example, it is said about enums in C#/.net. So, when is a feature considered a "First class citizen" in a programming language/platform?
An object is first-class when it:
- can be stored in variables and data structures
- can be passed as a parameter to a subroutine
- can be returned as the result of a subroutine
- can be constructed at runtime
- has intrinsic identity (independent of any given name)
The term "object" is used loosely here, not necessarily referring to objects in object-oriented programming. The simplest scalar data types, such as integer and floating-point numbers, are nearly always first-class.
The notion of "first-class citizen" or "first-class element" in a programming language was introduced by British computer scientist Christopher Strachey in the 1960s in the context of first-class functions. The most famous formulation of this principle is probably in Structure and Interpretation of Computer Programs by Gerald Jay Sussman and Harry Abelson:
- They may be named by variables.
- They may be passed as arguments to procedures.
- They may be returned as the results of procedures.
- They may be included in data structures.
Basically, it means that you can do with this programming language element everything that you can do with all other elements in the programming language.
It's all about "equal rights": you can do all of the above, with, say, integers, so why should any other thing be different?
The definition above is a bit restrictive in the sense that it only really talks about the aspect of first-classness as related to being objects of the program. A more general definition would be that a thing is first-class if you can do everything with it you can also do with other things of similar kind.
For example, Java operators and Java methods are of similar kind. You can define new methods, you can (somewhat) freely choose the names of your own methods, you can override methods, you can overload methods. James Gosling can do all of that with operators, too, but you and I can't. I mean, contrary to popular belief, Java does support operator overloading: for example, the
+ operator is overloaded for
String, and IIRC in Java 7 also for
BigDecimal (and probably a couple I forgot), it's just that you don't have any influence over it. That clearly makes operators second-class according to this second definition. Note that methods still aren't first-class objects according to the first definition, though. (Does that make operators third-class?)
Usually this refers to a construct that is passable as a parameter, can be defined as a return type from a function or can be assigned a value. Normally you need to be able to construct them at runtime. For example an instance of a class would be a first class citizen in c++ or java, but a function in C would not be.
I would say a feature is a first class citizen if it is implemented solely by the language.
i.e. it does not require multiple language features or a standard library to implement that feature.
In C/C++ I do not consider functions to be a first class citizen (others may).
This is because there are ways to manipulate functions that are nut supported directly by the language but require the use of other language features. Binding parameters to a function is not directly supported and you must build a functor to implement this feature.
To add an example to the answers already provided:
In WCF/C# you currently have to mark a class object with a service contract attribute to have it operate as a service. There is no such thing as:
public **service** MyService (in relation public **class** MyClass).
A class is a first class citizen in c#, where a service is not.
Hope this helps