Skip to content

Commit 5de8b7c

Browse files
authored
Add an entry for "top type" to the glossary (#7178)
Adds an entry for "top type" to the glossary, as we already have an entry for "bottom type", and I will reference this in upcoming type system documentation. **Staged:** https://dart-dev--pr7178-glossary-top-type-p2l0m99j.web.app/glossary/top-type Contributes to #4745 Contributes to #6461
1 parent 8055688 commit 5de8b7c

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

site/lib/src/pages/glossary.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ final class GlossaryCard extends StatelessComponent {
240240
],
241241
collapsedContent: [
242242
if (entry.shortDescription.isNotEmpty)
243-
DashMarkdown(content: entry.shortDescription, inline: true),
243+
DashMarkdown(content: entry.shortDescription),
244244
],
245245
expandedContent: [
246246
if (entry.longDescription case final longDescription?)

src/data/glossary.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,80 @@
18711871
alternate:
18721872
- "Dead-code elimination"
18731873

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+
18741948
- term: "Type alias"
18751949
short_description: |-
18761950
A user-defined name for an existing type.

0 commit comments

Comments
 (0)