In a programming language, what differentiates a first-class citizen from the rest?

8

In a simple way, what is a "first class citizen" in a programming language? And, most importantly, how does something that is a first class citizen differ from something that is not?

    
asked by anonymous 08.08.2017 / 20:22

1 answer

7

According to Wikipedia is when a feature of the language can be used in all the ways that it is usually possible to use in it.

If a resource can be created in the language but can not be assigned to a variable, or passed as an argument to a function, or returned, that particular resource is not a first-class citizen, it can not do everything the of first class can, there is limitation in its action.

The term is heavily used in functions and this is a good example. Essentially any language can create them, but not all can have them assigned to variables or passed as arguments or return. The languages that allow this have the role as a first class citizen.

It has languages that allow to do this but through a secondary mechanism obscure, that is, it can be done, but it is not because the language "consciously" decided to have it in a facilitated way and does not have a syntax of its own. So getting it is something collateral to the intent of the language, and porting is second class.

Not having a literal for it probably makes the feature second-rate.

In the article indicates that the term Christopher Strachey was created in 1960 probably to differentiate it from precarious ways. The term today is widely used as marketing to say that language has something better than others, even if it is not always the case.

I've seen it being used in slightly different contexts and I do not know if it's right. They look like "native" things in the language as being first class, as opposed to things you can do with some effort. For example asynchronism that is native in C # 5 up, although it has always given to make of convolute form. But you can not use async anywhere in the language, so it would not be first class by the more formal definition.

Some say that language must have the power to create a resource by its own code so that it can be considered first class. By this definition, it makes dynamic languages more prone to have first-class mechanisms. It seems to me that this proposition is a little more strict and not always accepted in all circles. This is not usually the case in the Structure and Interpretation of Computer Programs which is kind of a bible of computing.

But there are those who say that in the least strict definition it just means that the resource must be able to be used in anything in the language. I do not know if it's true because it's rare to have something that can be used so universally so it would need a definition of where it can or can not. No language is so free that something can be used at all. Like many terms, there is controversy.

In the answer from Norman Ramsey , who understands the subject well. it puts, for example, that a int in Java is not first class because it can not be inherited. It's a way of thinking.

Just do not think that having something as second class is inherently bad. It has things being first class sucks. It can give power, but it can give headache dealing with that.

    
08.08.2017 / 20:47