What is a first class programming construct?

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 (just before Exercise 1.40) 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.


I suspect you won't find a formal definition Apparently Jörg W Mittag found one :)

Given that formal definition, the rest of my answer is merely my understanding of it at the time. Whether everyone who uses the term "first-class construct" means exactly the same thing is a different matter, of course.

The way to determine whether something is a "first class" construct or not is to ask yourself something like this:

Is the feature supported and thoroughly integrated with the rest of the language, or are there a lot of unnecessary restrictions which give the impression that it's just been "bolted on" possibly to tackle just one particular use case without consideration for other areas where the construct could be really useful if it had been more fully "part of the language"?

As you can see, it's a definite grey area :)

Delegates in C# are a good example, actually. In C# 1 you could pass delegates into methods, and there were plenty of ways in which they were well integrated into the language (things like conversions being available, event handling, += and -= translating to Delegate.Combine/Remove). I'd say they were first class constructs. However, that doesn't contradict the fact that delegates have gained tremendously from C# 2 and 3, with anonymous methods, implicit method group conversions, lambda expressions and covariance. They're arguably more of a first class construct now... and even though I would say they were "first class" in C# 1 I could see why someone might disagree.

A similar case might be made for IEnumerable. In C# 1.0, it was supported by foreach but the foreach loop wouldn't dispose of the IEnumerator at the end. This part was fixed in C# 1.2, but there was still only language support for consuming IEnumerables, not creating them. C# 2.0 provided iterator blocks, which make it trivial to implement IEnumerable (and its generic equivalent). Does that mean the concept of an iterable sequence wasn't a "first class" construct in C# 1.0? Debatable, basically...

Tags:

.Net