What is a “First Class” type?

后端 未结 3 1580
故里飘歌
故里飘歌 2020-12-24 06:56

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

相关标签:
3条回答
  • 2020-12-24 07:25

    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.

    0 讨论(0)
  • 2020-12-24 07:28

    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.

    0 讨论(0)
  • 2020-12-24 07:42

    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.

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