The highest type (top type) in type theory , often denoted as simply a vertex or a “fixed” symbol (⊤), is a universal type, that is, a type that contains every possible object in the desired type system. The highest type is sometimes called the universal supertype , that is, all other types in any given type system are subtypes of the uppermost. This is the opposite of the lowest type , or otherwise called the universal subtype , which is a type that does not contain any nested members (that is, it does not have subtypes).
Support in programming languages
Some typed programming languages provide explicit support for the higher type.
| Title | Languages |
|---|---|
Object | Smalltalk , Java , JavaScript , Ruby and some others. In Java, it is often denoted with a prefix that points to a package, for example, java.lang.Object . It is also worth noting that Object not a supertype for primitive types in Java; however, starting with Java 1.5, autoboxing allows you to explicitly or implicitly cast a primitive to an Object , such as ((Object)42).toString() . |
Object | Objective-C . In OpenStep and Cocoa , which are the most popular language-based frameworks, NSObject is usually a universal base class. You can create new root classes simply without specifying the parent classes when defining, but this is highly discouraged. The highest type for pointers to an object is id . |
System.Object | C # , Visual Basic .NET, and other .NET Framework languages |
object | Python , starting with the unification of types / classes in version 2.2. |
GObject | Glib |
TObject | Object Pascal . [one] |
t | Common Lisp and many other Lisp dialects. |
Any | Scala . [2] , X10 . |
GENERAL | Eiffel . |
UNIVERSAL | Perl 5. |
Variant | Visual Basic up to version 6. |
C ++ stands out among object-oriented languages in that it does not have a universal supertype. The type "pointer to void" can take a pointer to any object, even though the void type is not a universal type in itself, but only a single type .
Object-oriented languages usually do not have a universal supertype (or fully support polymorphism of subtypes). Common Lisp is an exception in that its built-in types form a hierarchy of types, even when its CLOS object system is not used.
In most programming languages, the highest type is an abstract type that does not contain elements that are not elements of some (declared) subtype. In this case, Java is an exception: the expression new Object() is quite correct.
The higher type is used as a “generic” type, primarily in languages without support for parametric polymorphism . For example, before adding generic types to Java 5, a collection of classes in the Java library (excluding Java arrays) supported references to the Object type; with this method, any improper type can be added to the collection. The highest type is also often used to support objects of an unknown type.
In languages with a structural type system, the highest type is an empty structure. For example, objects in OCaml are structurally typed; the empty type of the object (the type of an object that has no methods), " < > ", is the highest type for object types. Any object in OCaml can be explicitly cast to this type, although the result can be quite useless.
The higher type can also be represented as an implicit type in languages with non-static typing. Languages with dynamic typing often support a downcast (or “type refinement”) to enable the programmer to specify a more specific type for an object during program execution. It should be noted that a downcast from type void * in C ++ cannot be performed in a “safe” style when an unsuccessful cast is detected by the language environment.
Notes
- Pierce, Benjamin C. Types and Programming Languages . - MIT Press , 2002. - ISBN 0-262-16209-1 .
Links
- c2.com: Top type
- Types: Top and Bottom (Russian)