What does it mean for a type T to be a "First Class" type?
3 Answers
Usually it means instances of T can be
- returned from functions
- passed into functions
- constructed at runtime
Eg functions in C are not first class types as they cannot be constructed at runtime, but they are in JavaScript.
In some specialised circumstances, for example theorem proving, it means that types themselves are first class objects. More modern literature uses 'reified types' instead to denote this to avoid such ambiguity.
-
9Umm... no. Although the OP selected this as the answer, this does not even come close to answering his question. He means type as in type theory. What you describe are other first class citizens, but none of them are types. -1. Here is one that answers it correctly, if only breifly: stackoverflow.com/questions/599978/what-is-a-first-class-type/… Nov 10, 2011 at 20:57
-
@trinithis This other answer you selected is an example of 'reified types', where the type 'type' itself is a first class type. For references see google.com/… Nov 10, 2011 at 21:52
-
2In support of @Pete Kirkham 's answer... In Expert C Programming (1994) Peter van der Linden gives the following example: /* array inside a struct*/ struct s_tag {int a[100]; }; He says that the array can now be treated as a first-class type. It can 1. be copied with an assignment statement 2. be passed to a function by value 3. be the return type of a function Dec 18, 2013 at 23:21
-
1This is not correct. This answer explains what a first class value is, but I think the op meant what you can do when your type (like Int or Float in python) itself can be treated as first class entities. Like pass a type into a function as an argument etc. AFAIK, only Coq implements first class types.– JaseemOct 19, 2014 at 2:24
-
1@Jaseem did you follow the google search link I gave showing how this is the usage of the phrase at it appears most commonly in the literature? 'first class type' is used to mean 'the type of a first class value' far more than as a reified type as it is in Coq. Arguing that this is not 'correct' is like saying it is incorrect to use 'outstanding' to mean 'exceedingly good' rather than 'not yet processed' Oct 19, 2014 at 15:26
The use of "T" make it sound like maybe someone was speaking about the state of generics in Java (they get erased, meaning that while you can check if something is a List at runtime, you can't check if it's a List of Integer).
However, there are also "first class types," meaning that types themselves (not just instances of them) can show up anywhere, like as the value of an expression. For instance, code snippets like
someType s = new someType();
new typeOf(s); // makes a new instance of someType
But you don't see that in the wild much, since if your types depend on a value, type-checking requires more computation, and if you allow types to depend on any value, then checking becomes undecidable.
-
-
-
Ha! I suppose that for this imaginary method of construction to have any value, you'd want
typeOf(s)
to return a class definition, to which you could pass constructor arguments. But it's been a long time & I don't remember what I intended. In retrospect, plain oldnew s()
or evenconstructObject(s, args)
seem better. Also, why did I call the types
instead oft
?– johncipFeb 16, 2019 at 23:34
I think a first-class type is about the same thing as a first-class object. It's basically the type which provides the properties of a first-class object.