|
1871 | 1871 | alternate: |
1872 | 1872 | - "Dead-code elimination" |
1873 | 1873 |
|
| 1874 | +- term: "Top type" |
| 1875 | + short_description: |- |
| 1876 | + A type that is a supertype of every other type, |
| 1877 | + including `Object?`. |
| 1878 | + long_description: |- |
| 1879 | + A **top type** is any type that is a supertype of `Object?`. |
| 1880 | + Because `Object?` is itself a supertype of every other Dart type, |
| 1881 | + a top type sits at the very top of the type hierarchy, |
| 1882 | + so you can assign any value to a variable with a top type. |
| 1883 | +
|
| 1884 | + Dart has many top types, |
| 1885 | + but the primary ones are the following three: |
| 1886 | +
|
| 1887 | + - **`Object?`** - |
| 1888 | + The nullable supertype of all types. |
| 1889 | + Every value in Dart, including `null`, is an `Object?`. |
| 1890 | +
|
| 1891 | + - **`dynamic`** - |
| 1892 | + A special type that disables static type checking. |
| 1893 | + Like `Object?`, it accepts any value, |
| 1894 | + but it also allows you to access _any_ member |
| 1895 | + on it without a compile-time error. |
| 1896 | + The check is deferred to runtime instead. |
| 1897 | +
|
| 1898 | + - **`void`** - |
| 1899 | + A type that indicates its value is not intended to be used. |
| 1900 | + You can assign any value to `void`, |
| 1901 | + but you can't use the result without a cast. |
| 1902 | +
|
| 1903 | + Beyond these primary three, there are other composite top types. |
| 1904 | + `FutureOr<T>` is a top type whenever `T` is a top type, |
| 1905 | + and adding `?` to certain types produces a top type as well. |
| 1906 | + For example, `FutureOr<Object?>`, `FutureOr<Object>?`, |
| 1907 | + and `dynamic?` are all top types. |
| 1908 | +
|
| 1909 | + In contrast to `Object?`, the non-nullable `Object` type is |
| 1910 | + the supertype of all _non-nullable_ types but is not a true top type |
| 1911 | + because it isn't a supertype of `Null`. |
| 1912 | +
|
| 1913 | + The top type is the converse of the [bottom type](#bottom-type). |
| 1914 | + While a top type is a supertype of everything, |
| 1915 | + the bottom type (`Never` in Dart) is a subtype of everything. |
| 1916 | +
|
| 1917 | + ```dart |
| 1918 | + Object? a = 42; // OK: int is a subtype of Object?. |
| 1919 | + Object? b = 'hello'; // OK: String is a subtype of Object?. |
| 1920 | + Object? c = null; // OK: Null is a subtype of Object?. |
| 1921 | + dynamic d = [1, 2, 3]; // OK: List<int> is a subtype of dynamic. |
| 1922 | + void e = true; // OK: bool is a subtype of void. |
| 1923 | + ``` |
| 1924 | + related_links: |
| 1925 | + - text: "The Dart type system" |
| 1926 | + link: "/language/type-system" |
| 1927 | + type: "doc" |
| 1928 | + - text: "Bottom type" |
| 1929 | + link: "#bottom-type" |
| 1930 | + type: "term" |
| 1931 | + - text: "Top and bottom types in Dart" |
| 1932 | + link: "/null-safety/understanding-null-safety#top-and-bottom" |
| 1933 | + type: "doc" |
| 1934 | + - text: "Syntactic characterization of top types in Dart" |
| 1935 | + link: "https://github.com/dart-lang/language/blob/main/accepted/2.12/nnbd/feature-specification.md#helper-predicates" |
| 1936 | + type: "external" |
| 1937 | + - text: "Top type on Wikipedia" |
| 1938 | + link: "https://en.wikipedia.org/wiki/Any_type" |
| 1939 | + type: "external" |
| 1940 | + labels: |
| 1941 | + - "language" |
| 1942 | + - "type system" |
| 1943 | + alternate: |
| 1944 | + - "Any type" |
| 1945 | + - "Universal type" |
| 1946 | + - "Universal supertype" |
| 1947 | + |
1874 | 1948 | - term: "Type alias" |
1875 | 1949 | short_description: |- |
1876 | 1950 | A user-defined name for an existing type. |
|
0 commit comments