Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Top type

From Wikipedia, the free encyclopedia
Universal type in logic and computer science

Inmathematical logic andcomputer science, sometype theories andtype systems include atop type that is commonly denoted withtop or the symbol ⊤. The top type is sometimes called alsouniversal type, oruniversal supertype as all other types in the type system of interest aresubtypes of it, and in most cases, it contains every possible object of the type system. It is in contrast with thebottom type, or theuniversal subtype, which every other type is supertype of and it is often that the type contains no members at all.

Support in programming languages

[edit]

Several typedprogramming languages provide explicit support for the top type.

Instatically-typed languages, there are two different, often confused, concepts when discussing the top type.

  1. Auniversal baseclass or other item at the top of aruntimeclass hierarchy (often relevant inobject-oriented programming) ortype hierarchy; it is often possible to create objects with this (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 todynamic 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 * inC++,id inObjective-C,interface {} inGo), static types which 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. 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.

Mostobject-oriented programming languages include a universalbase class:
NameLanguages
ObjectSmalltalk,JavaScript,Ruby (pre-1.9.2),[1] and some others.
java.lang.ObjectJava. Often written without the package prefix, asObject. Also, it isnot a supertype of the primitive types; however, since Java 1.5,autoboxing allows implicit or explicittype conversion of a primitive value toObject, e.g.,((Object)42).toString()
System.Object[2]C#,Visual Basic (.NET), and other.NET framework languages
std::anyC++ sinceC++17
objectPython since unifyingtype andclass in version 2.2[3] (new-style objects only; old-style objects in 2.x lack this as a base class). A new typing module introduces typeAny which is compatible with any type and vice versa
TObjectObject Pascal
tLisp, many dialects such asCommon Lisp
Any?Kotlin[4]
AnyScala,[5]Swift,[6]Julia,[7]Python[8]
ANYEiffel[9]
UNIVERSALPerl 5
VariantVisual Basic up to version 6,D[10]
interface{}Go
BasicObjectRuby (version 1.9.2 and beyond)
any andunknown[11]TypeScript (withunknown having been introduced in version 3.0[12])
mixedPHP (as of version 8.0)

The following object-oriented languages have no universal base class:

  • C++. Thepointer to void type can accept any non-function pointer, even though thevoid type is not the universal type but theunit type. Since C++17, thestandard library provides the top typestd::any.
  • 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.Object is conventionally used as the base class in the original Objective-C runtimes. In theOpenStep andCocoa Objective-Clibraries,NSObject is 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 protocolAny can accept any type.

Other languages

[edit]

Languages that are not object-oriented usually have no universal supertype, or subtypepolymorphism support.

WhileHaskell purposefully lacks subtyping, it has several other forms of polymorphism includingparametric polymorphism. The most generic type class parameter is an unconstrained parametera (without atype class constraint). InRust,<T: ?Sized> is the most generic parameter (<T> is not, as it implies theSized trait by default).

The top type is used as ageneric type, more so in languages withoutparametric polymorphism. For example, before introducing generics inJava 5, collection classes in the Java library (excluding Java arrays) held references of typeObject. 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.

The top type may also be seen as the implied type of non-statically typed languages. Languages with runtime typing often providedowncasting (ortype refinement) to allow discovering a more specific type for an object at runtime. In C++, downcasting fromvoid * cannot be done in asafe way, where failed downcasts are detected by the language runtime.

In languages with astructural type system, the empty structure serves as a top type. For example, objects inOCaml 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 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

[edit]

The notion oftop is also found inpropositional calculus, corresponding to a formula which is true in every possible interpretation. It has a similar meaning inpredicate calculus. Indescription 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 theWeb Ontology Language (OWL), which supports various description logics, top corresponds to the classowl:Thing, where all classes are subclasses ofowl:Thing. (the bottom type or empty set corresponds toowl:Nothing).

See also

[edit]

Notes

[edit]
  1. ^"Class: BasicObject (Ruby 1.9.2)". RetrievedApril 7, 2014.
  2. ^System.Object
  3. ^Python type/class unification
  4. ^Matilla, Hugo (2019-02-27)."Kotlin basics: types. Any, Unit and Nothing".Medium. RetrievedSeptember 16, 2019.
  5. ^"An Overview of the Scala Programming Language"(PDF). 2006. RetrievedApril 7, 2014.
  6. ^"Types — The Swift Programming Language (Swift 5.3)".docs.swift.org. RetrievedNovember 2, 2020.
  7. ^"Types · The Julia Language". RetrievedMay 15, 2021.
  8. ^"The Any type". 2022. RetrievedOctober 26, 2022.
  9. ^"Standard ECMA-367. Eiffel: Analysis, Design and Programming Language"(PDF). 2006. RetrievedMarch 10, 2016.
  10. ^"std.variant - D Programming Language".dlang.org. Retrieved2022-10-29.
  11. ^"The top types 'any' and 'unknown' in TypeScript".
  12. ^"The unknown Type in TypeScript". 15 May 2019.

References

[edit]

External links

[edit]
Uninterpreted
Numeric
Pointer
Text
Composite
Other
Related
topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Top_type&oldid=1239582915"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp