Search code examples
language-agnostic

What is a first class programming construct?


When trying to do something fairly advanced in C# (like some sort of hack), the concept of "first class" is raised.

For example, a method is a first class programming construct because you can do xyz with it (xyz is not what the method does, but what a method in general gives you, I can't remember what xyz was now), but in .NET 1.1 delegates were not able to be passed into methods because they were not first class programming constructs (I read something along these lines).

What exactly is a first class programming construct?


Solution

  • 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...