Top type
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 subtype
s of top. It is in contrast with the bottom type
, or the universal subtype, which is the type containing no members at all.
s provide explicit support for the top type.
Note that in statically-typed languages, there are two different, often confused, concepts when discussing the top type.
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 whose variables can accept any object value, but which do not reflect real runtime 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. In this article, we try to stick to the first concept when discussing top types, but also mention the second concept in languages where it is significant.
C++
is unusual among OO languages: it has no universal base class. 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
.
Objective-C
is another OO language with no universal base class -- it is possible to create a new base class by not specifying a parent class for a class, although this is highly unusual.
and Cocoa
, which are the most popular frameworks based on ObjC, NSObject is conventionally the universal base class. The top type for pointers to objects is id
PHP
is another OO language with no universal base class.
Non-OO languages usually have no 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.
The top type is used as a generic type, particularly in languages without parametric polymorphism
. For example, before the introduction of generics in Java 5, collection classes in the Java library (excluding Java arrays) held references of type
In languages with a structural type system
, the top type is the empty structure. For example, objects in Objective Caml
are structurally typed; the empty object type (the type of objects with no methods), "
also uses structural typing; and all types implement the empty interface:
The top type may also be seen as the implied type of non-statically typed languages. Languages with runtime typing often provide downcasting
(or type refinement) to allow the programmer to discover a more specific type for an object at run-time. Note that downcasting from
Type theory
In mathematics, logic and computer science, type theory is any of several formal systems that can serve as alternatives to naive set theory, or the study of such formalisms in general...
, 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 subtype
Subtype
In programming language theory, subtyping or subtype polymorphism is a form of type polymorphism in which a subtype is a datatype that is related to another datatype by some notion of substitutability, meaning that program constructs, typically subroutines or functions, written to operate on...
s of top. It is in contrast with the bottom type
Bottom type
In type theory, a theory within mathematical logic, the bottom type is the type that has no values. It is also called the zero or empty type, and is sometimes denoted with falsum .A function whose return type is bottom cannot return any value...
, or the universal subtype, which is the type containing no members at all.
Support in programming languages
Several typed programming languageProgramming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
s provide explicit support for the top type.
Note that 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 runtime class hierarchy (often relevant in object-oriented programming) or type hierarchy; it is often possible to create objects with this actual (runtime) 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 whose variables can accept any object value, but which do not reflect real runtime 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. In this article, we try to stick to the first concept when discussing top types, but also mention the second concept in languages where it is significant.
Name | Languages |
---|---|
Object |
Smalltalk Smalltalk Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist... , JavaScript JavaScript JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles.... , Ruby Ruby (programming language) Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto... , and some others. |
|
Java Java (programming language) Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities... . Frequently written without the package prefix, as Object. Note also that it is not a supertype of the primitive types; although, since Java 1.5, autoboxing allows implicit or explicit type conversion Type conversion In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations... of a primitive value to Object , e.g. ((Object)42).toString |
System.Object |
C#, Visual Basic .NET Visual Basic .NET Visual Basic .NET , is an object-oriented computer programming language that can be viewed as an evolution of the classic Visual Basic , which is implemented on the .NET Framework... and other .NET Framework .NET Framework The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability... languages |
object |
Python Python (programming language) Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive... since the type/class unification in Version 2.2 (new-style objects only; old-style objects in 2.x do not have this as a base class) |
Gobject |
Glib GLib GLib is a cross-platform software utility library that began as part of the GTK+ project. However, before releasing version 2 of GTK+, the project's developers decided to separate non-GUI-specific code from the GTK+ platform, thus creating GLib as a separate product... |
TObject |
Object Pascal Object Pascal Object Pascal refers to a branch of object-oriented derivatives of Pascal, mostly known as the primary programming language of Embarcadero Delphi.-Early history at Apple:... http://www.delphibasics.co.nr/RTL.asp?Name=TObject |
t |
Lisp, many dialects such as Common Lisp Common Lisp Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers... |
Any |
Scala http://www.scala-lang.org/docu/files/ScalaOverview.pdf |
GENERAL |
Eiffel Eiffel (programming language) Eiffel is an ISO-standardized, object-oriented programming language designed by Bertrand Meyer and Eiffel Software. The design of the language is closely connected with the Eiffel programming method... |
UNIVERSAL |
Perl Perl Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular... 5 |
Variant |
Visual Basic Visual Basic Visual Basic is the third-generation event-driven programming language and integrated development environment from Microsoft for its COM programming model... up to version 6 |
interface{} |
Go Go (programming language) Go is a compiled, garbage-collected, concurrent programming language developed by Google Inc.The initial design of Go was started in September 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. Go was officially announced in November 2009. In May 2010, Rob Pike publicly stated that Go was being... |
C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
is unusual among OO languages: it has no universal base class. The "pointer to void" type can accept a pointer to any object, even though the void type
Void type
The void type, in several programming languages derived from C and Algol68, is the type for the result of a function that returns normally, but does not provide a result value to its caller. Usually such functions are called for their side effects, such as performing some task or writing to their...
itself is not the universal type but the unit type
Unit type
In the area of mathematical logic, and computer science known as type theory, a unit type is a type that allows only one value . The carrier associated with a unit type can be any singleton set. There is an isomorphism between any two such sets, so it is customary to talk about the unit type and...
.
Objective-C
Objective-C
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...
is another OO language with no universal base class -- 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 runtimes. In the OpenStepOpenStep
OpenStep was an object-oriented application programming interface specification for an object-oriented operating system that used a non-NeXTSTEP operating system as its core, principally developed by NeXT with Sun Microsystems. OPENSTEP was a specific implementation of the OpenStep API developed...
and Cocoa
Cocoa (API)
Cocoa is Apple's native object-oriented application programming interface for the Mac OS X operating system and—along with the Cocoa Touch extension for gesture recognition and animation—for applications for the iOS operating system, used on Apple devices such as the iPhone, the iPod Touch, and...
, which are the most popular frameworks based on ObjC, NSObject is conventionally the universal base class. The top type for pointers to objects is id
PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...
is another OO language with no universal base class.
Non-OO languages usually have no 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.
The top type is used as a generic type, particularly in languages without parametric polymorphism
Parametric polymorphism
In programming languages and type theory, parametric polymorphism is a way to make a language more expressive, while still maintaining full static type-safety. Using parametric polymorphism, a function or a data type can be written generically so that it can handle values identically without...
. For example, before 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 languages with a structural type system
Structural type system
A structural type system is a major class of type system, in which type compatibility and equivalence are determined by the type's structure, and not by other characteristics such as its name or place of declaration. Structural systems are used to determine if types are equivalent and whether a...
, the top type is the empty structure. For example, objects in Objective Caml
Objective Caml
OCaml , originally known as Objective Caml, is the main implementation of the Caml programming language, created by Xavier Leroy, Jérôme Vouillon, Damien Doligez, Didier Rémy and others in 1996...
are structurally typed; the empty object type (the type of objects with no methods), "
< >
", is the top type of object types. Any OCaml object can be explicitly upcasted to this type, although the result would be pretty useless. GoGo (programming language)
Go is a compiled, garbage-collected, concurrent programming language developed by Google Inc.The initial design of Go was started in September 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. Go was officially announced in November 2009. In May 2010, Rob Pike publicly stated that Go was being...
also uses structural typing; and all types implement the empty interface:
interface{}
.The top type may also be seen as the implied type of non-statically typed languages. Languages with runtime typing often provide downcasting
Downcasting
In object-oriented programming, downcasting or type refinement is the act of casting a reference of a base class to one of its derived classes....
(or type refinement) to allow the programmer to discover a more specific type for an object at run-time. Note that downcasting from
void *
in C++ cannot be done in a "safe" manner, where failed downcasts are detected by the language runtime.