Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a first class programming construct?

Tags:

.net

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?

Thanks

like image 465
GurdeepS Avatar asked Mar 14 '09 21:03

GurdeepS


People also ask

What is a first-class object in programming?

first-class object (plural first-class objects) (programming, languages) An entity that can be constructed at run-time, passed as a parameter, returned from a function, or assigned into a variable.

What is meant by construct in programming?

Programs are designed using common building blocks, known as programming constructs. These programming constructs form the basis for all programs. Computer Science. Computational thinking and problem solving.

Which programming languages have first-class functions?

Functional programming languages, such as Erlang, Scheme, ML, Haskell, F#, and Scala, all have first-class functions. When Lisp, one of the earliest functional languages, was designed, not all aspects of first-class functions were then properly understood, resulting in functions being dynamically scoped.

How do you define first class citizens in functional programming languages?

First-class citizenship, within the world of programming, means that a given entity (such as a function) supports all the operational properties inherent to other entities; properties such as being able to be assigned to a variable, passed around as a function argument, returned from a function, etc.


2 Answers

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.

like image 80
Jörg W Mittag Avatar answered Sep 27 '22 23:09

Jörg W Mittag


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

like image 45
Jon Skeet Avatar answered Sep 28 '22 00:09

Jon Skeet