Top type
From Wikipedia, the free encyclopedia
The top type in type theory, commonly abbreviated as top or by the down tack symbol (⊤) is the universal type--that type which contains every possible object in the type system of interest. The top type is sometimes called the universal supertype as all other types in any given type system are subtypes of top. It is in contrast with the bottom type, or the universal subtype, which is the type containing no members at all.
[edit] Support in programming languages
Several typed programming languages provide explicit support for the top type.
Most object oriented programming languages include a universal base class:
- Object in Smalltalk and Java. In the latter language, it is frequently prefixed written with a package designation, as java.lang.Object. Note also that Object is not a supertype of the primitive types in Java.
- type in Python since the type/class unification in Version 2.2. Note that classes are supposed to be based on object instead, while probably only Metaclasses will want to inherit from type.
- GENERAL in Eiffel.
- t in Common Lisp and many other Lisp dialects.
C++ is unusual among OO languages in that it does not have a universal supertype. The "pointer to void" type can accept a pointer to any object, even though the void type itself is not the universal type but the unit type.
Non-OO languages usually do not have a universal supertype (or support subtype polymorphism at all). Common Lisp is an exception in that its built-in types form a type hierarchy even when its object system CLOS is not used.
In most programming languages, the top type is an abstract type--it contains no elements which are not elements of some (declared) subtype. Java is an exception: the phrase new Object is perfectly legal Java.
The top type is used as a generic type, particularly in languages without parametric polymorphism. For example, prior to the introduction of 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 frequently used to hold objects of unknown type.
In statically typed languages which support top, downcasting (or type refinement) is frequently provided as a language feature to allow the programmer to discover a more specific type for an object. Note that downcasting from void * in C++ cannot be done in a "safe" manner, where failed downcasts are detected by the language runtime.
[edit] External links
- Types and Programming Languages by Benjamin Pierce (MIT Press 2002) [1]
- c2.com: Top type