Any type
In type theory and computer science, type systems include a top, universal, or any type, which includes all other types as subtypes. The top type is sometimes called also universal type, or universal supertype as all other types in the type system of interest are subtypes of it. In object-oriented languages, it is often called Object, as it represents all possible objects.
The top type contrasts with the bottom type, or the universal subtype, which every other type is the supertype of. The bottom type is a type that contains no subtypes 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.
- A universal base class or other item at the top of a runtime 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
- A static type in the code whose variables can be assigned any value, similar to dynamic typing
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.
| Name | Languages |
Object | Smalltalk, JavaScript 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.Object | C#, Visual Basic, and other.NET framework languages |
std::any | C++ since C++17 |
std::any::Any | Rust |
object | Python since unifying type and class in version 2.2. A new typing module introduces type Any which is compatible with any type and vice versa |
TObject | Object Pascal |
t | Lisp, many dialects such as Common Lisp |
Any? | Kotlin |
Any | Scala, Swift, Julia, Python |
ANY | Eiffel |
UNIVERSAL | Perl 5 |
Variant | Visual Basic up to version 6, D |
interface or any | Go |
BasicObject | Ruby |
any and unknown | TypeScript |
mixed | PHP |
The following object-oriented languages have no universal base class:
- C++ has no universal class like
Object. - 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.
Objectis conventionally used as the base class in the original Objective-C runtimes. In the OpenStep and Cocoa Objective-C libraries,NSObjectis conventionally the universal base class. The top type for pointers to objects isid. - Swift. It is possible to create a new base class by not specifying a parent class for a class. The protocol
Anycan accept any type.Other languages
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. Java supports "wildcard type parameters", represented by ?. For instance, List> denotes a list that can hold any type. Wildcards can be bounded, List extends T> and List super T> denote a list that can hold any type that inherits from/is an ancestor class of type T. Wildcards however cannot simultaneously have both upper and lower bounds.The top type may also be seen as the implied type of non-statically typed languages. Languages with runtime typing often provide downcasting to allow discovering a more specific type for an object at runtime.
In C and C++, the void pointer type can accept any non-function pointer, even though the void type is not the universal type but the unit type. In C++, downcasting from
void* cannot be done in a safe way, where failed downcasts are detected by the language runtime.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, linking 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 several description logics, top corresponds to the classowl:Thing, where all classes are subclasses of owl:Thing..