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.
1 Support in programming languages
1.1 Other languages
2 In logic 3 See also 4 Notes 5 References 6 External links
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.
A universal base class or other item at the top of a run time class hierarchy (often relevant in object-oriented programming) or type hierarchy; it is often possible to create objects with this (run time) type, or it could be found when one examines the type hierarchy programmatically, in languages that support it A (compile time) static type in the code whose variables can be assigned any value (or a subset thereof, like any object pointer 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 (e.g., void * in C++, id in Objective-C, interface in Go), 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 (any value can be assigned to any variable anyway), so only the first (class hierarchy) 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.
Most object-oriented programming languages include a universal base class:
java.lang.Object 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., ((Object)42).toString()
object Python since the type/class unification in version 2.2 (new-style objects only; old-style objects in 2.x lack this as a base class)
TObject Object Pascal
t Lisp, many dialects such as Common Lisp
BasicObject Ruby (version 1.9.2 and beyond)
The following object-oriented languages have no universal base class:
C++. The pointer to void type can accept any non-function pointer, even though the void type itself is not the universal type but the bottom type. Objective-C. It is possible to create a new base class by not specifying a parent class for a class, although this is highly unusual. Object is conventionally used as the base class in the original Objective-C run times. In the OpenStep and Cocoa Objective-C libraries, NSObject is conventionally the universal base class. The top type for pointers to objects is id. Swift. It is possible to create a new base class by not specifying a parent class for a class. The protocol Any can accept any type. PHP.
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 forall a. a. Similarly in Rust,
<T: ?Sized> is the most generic parameter (<T> is
not, as it implies the Sized trait).
The top type is used as a generic type, more so in languages without
parametric polymorphism. For example, before introduing generics in
Java 5, collection classes in the Java library (excluding Java arrays)
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.
In languages with a structural type system, the top type is the empty
structure. For example, objects in
Singly rooted hierarchy
^ "Class: BasicObject (Ruby 1.9.2)". Retrieved April 7, 2014. ^ System.Object ^ Python type/class unification ^ "An Overview of the Scala Programming Language" (PDF). 2006. Retrieved April 7, 2014. ^ "Standard ECMA-367. Eiffel: Analysis, Design and Programming Language" (PDF). 2006. Retrieved March 10, 2016.
Pierce, Benjamin C. (2002). Types and Programming Languages. MIT Press. ISBN 0-262-16209-1.
c2.com: Top type
v t e
Arbitrary-precision or bignum Complex Decimal Fixed point Floating point
Double precision Extended precision Half precision Long double Minifloat Octuple precision Quadruple precision Single precision
Algebraic data type
Array Associative array Class Dependent Equality Inductive List Object
Option type Product Record Set Union
Boolean Bottom type Collection Enumerated type Exception Function type Opaque data type Recursive data type Semaphore Stream Top type Type class Unit type Void
Abstract data type Data structure Generic Kind
Parametric polymorphism Primitive data type Protocol
Subtyping Type constructor Type conversion Type system Type theory
See also platform-dependent and independent un