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?
-
BTW: the term "first class citizen" is derecommended e.g. by the google styleguide for inclusive language as a socially charged term: https://developers.google.com/style/inclusive-documentation – tkruse Feb 24 '20 at 09:21
-
@tkruse What is "derecommended"? – Gherman Jun 08 '21 at 12:26
6 Answers
Definition
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.

- 2,541
- 21
- 11
-
1
-
7@Gulshan - you could argue the lack of intrinsic identity - C# enums are basically just syntactic sugar (i.e. a "given name") for an integer value. Compare with Java, where the enums are objects in their own right. – mikera Jan 25 '11 at 08:24
-
@mikera, in .NET enums are values in their own right. Java just does not have any values, only objects, that's the only difference. – SK-logic Jan 25 '11 at 11:08
-
@mikera: Though that prevents Java's enums from having some nice properties such as being able to represent bit fields with them. While their implementation is probably more first-class-y most of their APIs still has plenty of integer (or string) constants and many uses of those cannot easily be replaced with enums. – Joey Jan 25 '11 at 13:37
-
I don't think enums can be constructed at runtime in .Net, can they? I thought they were always constants. – travis Jan 25 '11 at 15:54
-
@travis, you can construct any value or object type in runtime in .NET – SK-logic Jan 25 '11 at 16:15
-
@SK-logic oh, I was thinking it meant definition, like: `var someNewEnum = enum { val1 = 1, val2 = 2 };` – travis Jan 25 '11 at 16:21
-
@travis You _can_ assign classes/structs/enums to variable in C# and Java. In C#, you use the `typeof` operator: `Type foo = typeof({className})`. Not sure how to do it in Java (been a while), but it involves the `Class` class (don't know package). If you mean creating one _at runtime_, I know .NET makes that possible with reflection, but I don't know how. I don't know about Java though. For you example, you'd be doing something similar to C/C++'s anonymous types. AFAIK, .NET _and_ Java don't support _that_ kind of anonymous types; you're better off just making another class instead. – Cole Tobin Feb 12 '15 at 17:30
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 byte
, short
, int
, long
, float
, double
and String
, and IIRC in Java 7 also for BigInteger
and 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?)

- 101,921
- 24
- 218
- 318
-
Great answer. So, for example, the [pipenv docs](https://pipenv.pypa.io/en/latest/#) say "Windows is a first-class citizen, in our world." Would this imply you can do everything with `pipenv` on Windows that you can also do with it on Linux? – djvg Jan 08 '21 at 11:58
-
2No, I don't think that usage of the term "first-class citizen" has anything with to do with the *formal Programming Language Theory* definition of the term. I think pipenv are just using the term with its normal standard English meaning. – Jörg W Mittag Jan 08 '21 at 12:02
-
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.

- 5,385
- 3
- 21
- 41
-
-
2@bjarkef: Sounds like that was already answered by its matching the description offered in the preceding sentences. – doppelgreener Jan 25 '11 at 07:47
-
@Jonathan: Yes, sorry, I misread the "construct them at runtime". Yes you can construct an instance of a class at runtime (an object), but not the class itself. That is what confused me. – Bjarke Freund-Hansen Jan 25 '11 at 07:53
-
1Passing by parameter is still not enough. In C/C++ I would still consider functions as second class citizens. They can be passed as parameters, returned as results placed inside other objects. But they can not be manipulated without the help of other constructs (like std::bind is required to bind parameters to a function). – Martin York Jan 25 '11 at 08:13
-
@Martin I never said that functions were first class citizens in C/C++. – Pemdas Jan 25 '11 at 15:21
-
@Pemdas: No you did not. I was commenting on your definition. By your definition above in C/C++ a function is a first class system. In my opinion functions in C/C++ are not a still not a first class citizen. Thus extrapolating from that; I am saying your definition is a good starting point but is not complete. (I am not saying I know what more you need in the definition but there is something more that is needed). – Martin York Jan 25 '11 at 16:20
-
Actually, I said that you have to be able to construct or instantiated first class citizens at runtime, which would imply that functions are not included. – Pemdas Jan 25 '11 at 16:43
-
@LokiAstari Functions are definitely first class in C++. You can store (references to) them in variables, you can pass (references to) them to functions, you can store (references to) them in data structures, and they have value independent of their name. You can even write `[] () { /* anonymous functions */ }`. – Miles Rout Feb 19 '16 at 06:51
-
@MilesRout: Takes more than the ability to store references to them. You need anonymous functions (otherwise there usage is not the same as say an int which you can use anonymously without a named value). So C++11 does have first class functions (with lambdas) but C++03/C false slightly short. Comments were made before the release of C++11. – Martin York Feb 19 '16 at 21:16
-
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.
Example:
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.

- 11,150
- 2
- 42
- 70
-
1Wouldn't that make bound functions (or "closures") not be first-class, while functions themselves are? How does 0x's support for closures factor in to your analysis? – Fred Nurk Jan 25 '11 at 10:19
-
@Fred Nurk: It all depends on the language. In some languages closures are first class systems. In others not. I am not familiar enough with C++0x yet to make an explicit comment. – Martin York Jan 25 '11 at 16:18
-
Let's say the language is either C or C++ (but not 0x), as in you answer. Wouldn't your definition of "first-class" make bound functions (or "closures") not be first-class, while functions themselves are? – Fred Nurk Jan 26 '11 at 01:03
-
@Fred Nurk: If you limit the only thing you can do with a function is make them a closure, then sure. But to me that's like saying if you platform supports integers addition only by importing a library. Then integers are first class citizens but addition of integers is not considered. In my view closure is an operation that can be performed on a function that effectively returns a new function (but it depends how you define it). But closure and binding are only two operations how many others are we excluding from the discussion (I am not sure that was a question). – Martin York Jan 26 '11 at 01:16
-
@Martin: I must not be explaining myself clearly. Given "a feature is a first class citizen if it is implemented solely by the language", then functions in both C and C++ are implemented solely by the language and would thus be first-class. Bound functions (which can also be called "closures") are what you're talking about with binding parameters, etc., but that's a different feature. – Fred Nurk Jan 26 '11 at 01:19
-
If you were to say C and C++ functions are not first class because some things you might want to do with them (e.g. binding parameters) need library support, then why doesn't this apply to, for example, doubles, since I need library support to take their square root? (Library support or, as you said, "multiple language features" such as loops, in order to write my own sqrt function.) – Fred Nurk Jan 26 '11 at 01:22
-
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

- 754
- 1
- 4
- 8
‘First-class’ is a meaningless buzzword when it comes to programming language features. Consider:
- ‘First-class functions’ means functions are values like any other and can be passed as arguments, returned, stored in variables, etc.
- ‘First-class’ anything else usually means the feature has dedicated syntax almost, but not quite entirely unlike anything else in the language and has to be considered separately when reasoning about the code.
This term has no consistent meaning other than ‘someone spent a lot of time thinking about this’. Just avoid it entirely. Your thinking will be much clearer for it.

- 215
- 1
- 9