Top Type - Support in Programming Languages

Support in Programming Languages

Several typed programming languages 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.

  1. 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
  2. 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.

Most object-oriented programming languages include a universal base class:
Name Languages
Object Smalltalk, JavaScript, Ruby, and some others.
java.lang.Object Java. 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 of a primitive value to Object, e.g. ((Object)42).toString
System.Object C#, Visual Basic .NET and other .NET Framework languages
object Python 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
TObject Object Pascal
t Lisp, many dialects such as Common Lisp
Any Scala
GENERAL Eiffel
UNIVERSAL Perl 5
Variant Visual Basic up to version 6
interface{} Go

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. Object is conventionally used as the base class in the original Objective-C runtimes. In the OpenStep 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).

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 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, the top type is the empty structure. For example, objects in OCaml 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. Go 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 (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.

Read more about this topic:  Top Type

Famous quotes containing the words support, programming and/or languages:

    In the middle years of childhood, it is more important to keep alive and glowing the interest in finding out and to support this interest with skills and techniques related to the process of finding out than to specify any particular piece of subject matter as inviolate.
    Dorothy H. Cohen (20th century)

    If there is a price to pay for the privilege of spending the early years of child rearing in the driver’s seat, it is our reluctance, our inability, to tolerate being demoted to the backseat. Spurred by our success in programming our children during the preschool years, we may find it difficult to forgo in later states the level of control that once afforded us so much satisfaction.
    Melinda M. Marshall (20th century)

    People in places many of us never heard of, whose names we can’t pronounce or even spell, are speaking up for themselves. They speak in languages we once classified as “exotic” but whose mastery is now essential for our diplomats and businessmen. But what they say is very much the same the world over. They want a decent standard of living. They want human dignity and a voice in their own futures. They want their children to grow up strong and healthy and free.
    Hubert H. Humphrey (1911–1978)