What is a first class programming construct?

前端 未结 6 864
误落风尘
误落风尘 2020-12-24 01:59

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 programm

相关标签:
6条回答
  • 2020-12-24 02:31

    What exactly is a first class programming construct?

    Something is a first-class construct if the language supports it in a way that is analogous to other kinds of objects.

    For example, in C++, functions would not be considered first-class constructs: you can make other kinds of objects at runtime, but you can't instantiate new functions. By contrast, in C# 3 and later, functions would probably be considered first-class constructs with the introduction of lambdas/anonymous functions.

    As with a great many other things, this is naturally a subjective definition. Wikipedia has a good summary for more concrete examples, as does C2.

    0 讨论(0)
  • 2020-12-24 02:43

    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.

    0 讨论(0)
  • 2020-12-24 02:44

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

    0 讨论(0)
  • 2020-12-24 02:46

    My understanding of a first-class programming construct is that it means you can do the thing, whatever that might be, in the best way possible without having to do two or three other things, or to combine two or three other concepts.

    So, Java doesn't have first-class closure support. We can somewhat fake it however by building a class (using another concept) that supports nearly all of the stuff you use closures for and then creating instances of that class (another concept) or anonymously extending that class (yet another concept).

    Same with 'functions'. In Java you can't have functions, but you can have static methods, which are a similar but still different concept.

    One (not me) could also argue that Java's generics support is not first class, because it's a library hack and isn't actually "in" the language. This means that some of the things you can do in C# with generics you cannot do in Java.

    0 讨论(0)
  • 2020-12-24 02:47

    What exactly is a first class programming construct?

    Simple answer: it's something that supports all the standard operations the language provides. In an object-oriented language, the first class constructs are, as you might expect, the objects. It turns out that in such languages there are often, for efficiency or legacy reasons, second class citizens, typically the primitive data types, that aren't proper objects, and for which you can't do certain things with.

    For example, in Java a Point is a full fledged object. You can declare a

    List<Point> plist;

    and then put points into it. An int is not a full fledged object. There are certain things you can't do with an int -- subclass from it, for example, or invoke methods on it aside from some predefined operators. Nor can you store it in a

    List<int> intlist;

    Java has an out for this problem in that there is a "boxed" type, Integer, which is a full fledged object. You can declare a List<Integer>, for example. However, using an Integer is far less efficient than with an "int." Java provides automatic boxing and unboxing (that is to say, conversion) of the Integer type to int and back again in contexts where one or other is required, to help the programmer combine the best of both worlds.

    Having two types -- Integer and int -- for an integer number is confusing, and something of a hack. An early version of Java, I am told, had no primitives and all its types were full-fledged objects. That turned out to be too slow for its intended use (early Java turned out to be slow anyway, of course!) so some primitives that did not support proper object semantics were added.

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

    I know nothing about .NET, but it seems like you have the idea. You're right -- if delegates can't be passed into methods like any other standard object, they aren't first class programming constructs.

    0 讨论(0)
  • 2020-12-24 02:51

    The C2 wiki has a good entry on first class language constructs. Basically, it's stuff that you don't have to fake within a language.

    0 讨论(0)
提交回复
热议问题