Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a "First Class" type?

Tags:

types

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

like image 469
MrDatabase Avatar asked Mar 01 '09 15:03

MrDatabase


People also ask

What is a first class concept?

In database modeling, a first class item is one that has an identity independent of any other item. The identity allows the item to persist when its attributes change, and allows other items to claim relationships with the item. As a general rule, first class items represent things rather than relationships.

What does first class mean in programming?

A programming language is said to have First-class functions when functions in that language are treated like any other variable. For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable.

What makes someone a first-class citizen?

Noun. first-class citizen (plural first-class citizens) A member of a class of individuals that receive fair treatment.

What is a first class entity?

A first-class object is an entity within a programming language that can: Appear in an expression. Be assigned to a variable. Be used as an argument. Be returned by a function call.


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

like image 135
Pete Kirkham Avatar answered Oct 02 '22 10:10

Pete Kirkham


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.

like image 40
johncip Avatar answered Oct 02 '22 11:10

johncip