30

What does it mean for a type T to be a "First Class" type?

MrDatabase
  • 43,245
  • 41
  • 111
  • 153

3 Answers3

30

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.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • 11
    Umm... 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: http://stackoverflow.com/questions/599978/what-is-a-first-class-type/5839749#5839749 – Thomas Eding Nov 10 '11 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 https://www.google.com/search?tbm=bks&tbo=p&hl=en&q=%22first%20class%20type%22#ds=bo&pq=%22first+class+type%22&hl=en&cp=31&gs_id=1f&xhr=t&q=%22first+class+type%22+programming&tok=xD4ITLhnaS62HiM6AQS20g&pf=p&sclient=psy-ab&tbm=bks&source=hp&pbx=1&oq=%22first+class+type%22+programming+&aq=f&aqi=&aql=&gs_sm=&gs_upl=&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=3a82ac68bcbcfab4&biw=1024&bih=514 – Pete Kirkham Nov 10 '11 at 21:52
  • 2
    In 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 – GnomeDePlume Dec 18 '13 at 23:21
  • 1
    This 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. – Jaseem Oct 19 '14 at 02: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' – Pete Kirkham Oct 19 '14 at 15:26
  • @Jaseem - actually, in Smalltalk, classes are first class objects, defining types at runtime. However, Smalltalk is dynamically-typed, so no static type-checking occurs. I didn't realise Coq supported first-class types, and will have to take a look as it is something I have often thought about. – Rob G Nov 27 '16 at 22:45
28

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.

johncip
  • 2,087
  • 18
  • 24
  • I think @johncip meant `someType s = new someType();` `new typeOf(s);` – Endama Mar 30 '17 at 00:59
  • Shouldn't it be `new typeOf(s)();`? – Jozef Mikušinec Feb 15 '19 at 20:53
  • 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 old `new s()` or even `constructObject(s, args)` seem better. Also, why did I call the type `s` instead of `t`? – johncip Feb 16 '19 at 23:34
1

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.

strager
  • 88,763
  • 26
  • 134
  • 176