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

Asked

Viewed 632 times

11

In a simple way, what is a "first class citizen" in a programming language? And mainly, how do you differentiate something that is a first-class citizen of something that is not?

1 answer

11


According to Wikipedia is when a language feature allows it to be used in all ways that it is normally possible to use in it.

If a resource can be created in the language but cannot be assigned to a variable, or passed as argument from a function, or returned, that specific resource is not a first-class citizen, it cannot do everything that first-class ones can, there is limitation in its performance.

The term is widely used in functions And that’s a good example. Essentially any language can create them, but not all can have them assigned to variables or passed as arguments or returns. The languages that allow this have the function as a first-class citizen.

It has languages that even allow to do this but through a secondary obscure mechanism, that is, it can be done, but it is not because the language "consciously" decided to have it in an easy way and does not have a proper syntax for it. So getting it done is collateral to the intent of the language, and so it’s second class.

Not having a literal for that probably makes the resource second class, even if it exists.

The article indicates that the term was coined by Christopher Strachey in 1960 probably to differentiate from precarious forms. 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 used in slightly different contexts and I don’t know if it’s right. They see as "native" things in language as being first class, as opposed to things you can do with some effort. For example asynchronmentalism that is native in C# 5 up, although it has always been possible to do convoluted form. But you can not use async anywhere in the language, so it wouldn’t be first-class by the more formal definition.

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

But there are those who say that in the less strict definition only puts that the resource should be able to be used in anything of language. I don’t know if it’s right because it’s rare to have something that can be used so universally like that, you’d need a definition of where you can and can’t. No language is so free that something can be used at all. Like many terms, there is controversy.

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

Just don’t think having something as second class is inherently bad. There are things that being first class is terrible. It may give power, but it can give a headache to deal with it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.