Top type


The top type in the type theory of mathematics, logic, and computer science, commonly abbreviated as top or by the down tack symbol, is the universal type, sometimes called the universal supertype as all other types in any given type system are subtypes of top. In most cases it is the type which contains every possible object in the type system of interest. It is in contrast with the bottom type, or the universal subtype, which every other type is supertype of and in most cases it is the type that contains no members at all.

Support in programming languages

Several typed programming languages provide explicit support for the top type.
In statically-typed languages, there are two different, often confused, concepts when discussing the top type.
  1. A universal base class or other item at the top of a run time class hierarchy or type hierarchy; it is often possible to create objects with this type, or it could be found when one examines the type hierarchy programmatically, in languages that support it
  2. A static type in the code whose variables can be assigned any value, similar to dynamic typing
The first concept often implies the second, i.e., if a universal base class exists, then a variable that can point to an object of this class can also point to an object of any class. However, several languages have types in the second regard above, static types which variables can accept any object value, but which do not reflect real run time types that an object can have in the type system, so are not top types in the first regard.
In dynamically-typed languages, the second concept does not exist, so only the first is discussed. This article tries to stay with the first concept when discussing top types, but also mention the second concept in languages where it is significant.
NameLanguages
ObjectSmalltalk, JavaScript, Ruby, and some others.
Java. Often written without the package prefix, as Object. Also, it is not a supertype of the primitive types; however, since Java 1.5, autoboxing allows implicit or explicit type conversion of a primitive value to Object, e.g., .toString
System.ObjectC#, Visual Basic.NET, and other.NET Framework languages
objectPython since the type/class unification in version 2.2
TObjectObject Pascal
tLisp, many dialects such as Common Lisp
Any?Kotlin
AnyScala
ANYEiffel
UNIVERSALPerl 5
VariantVisual Basic up to version 6
interfaceGo
BasicObjectRuby
any and unknownTypeScript

The following object-oriented languages have no universal base class:
Languages that are not object-oriented usually have no universal supertype, or subtype polymorphism support.
While Haskell purposefully lacks subtyping, it has several other forms of polymorphism including parametric polymorphism. The most generic type class parameter is an unconstrained parameter a. In Rust, is the most generic parameter.
The top type is used as a generic type, more so in languages without parametric polymorphism. For example, before introducing generics in Java 5, collection classes in the Java library held references of type Object. In this way, any non-intrinsic type could be inserted into a collection. The top type is also often used to hold objects of unknown type.
The top type may also be seen as the implied type of non-statically typed languages. Languages with run time typing often provide downcasting to allow discovering a more specific type for an object at run time. In C++, downcasting from void * cannot be done in a safe way, where failed downcasts are detected by the language run time.
In languages with a structural type system, the empty structure serves as a top type. For example, objects in OCaml are structurally typed; the empty object type, < >, is the top type of object types. Any OCaml object can be explicitly upcasted to this type, although the result would be of no use. Go also uses structural typing; and all types implement the empty interface: interface , which has no methods, but may still be downcast back to a more specific type.

In logic

The notion of top is also found in propositional calculus, corresponding to a formula which is true in every possible interpretation. It has a similar meaning in predicate calculus. In description logic, top is used to refer to the set of all concepts. This is intuitively like the use of the top type in programming languages. For example, in the Web Ontology Language, which supports various description logics, top corresponds to the class owl:Thing, where all classes are subclasses of owl:Thing..