diff --git a/firebase.json b/firebase.json index 56721a2107..caab93192f 100644 --- a/firebase.json +++ b/firebase.json @@ -266,7 +266,7 @@ { "source": "/keyword/class", "destination": "/language/classes#instance-variables", "type": 301 }, { "source": "/keyword/const", "destination": "/language/variables#final-and-const", "type": 301 }, { "source": "/keyword/continue", "destination": "/language/loops#break-and-continue", "type": 301 }, - { "source": "/keyword/covariant", "destination": "/deprecated/sound-problems#the-covariant-keyword", "type": 301 }, + { "source": "/keyword/covariant", "destination": "/language/type-system#covariant-keyword", "type": 301 }, { "source": "/keyword/default", "destination": "/language/branches#switch", "type": 301 }, { "source": "/keyword/deferred", "destination": "/language/libraries#lazily-loading-a-library", "type": 301 }, { "source": "/keyword/do", "destination": "/language/loops#while-and-do-while", "type": 301 }, diff --git a/src/_data/glossary.yml b/src/_data/glossary.yml index 8a0bced844..16dad21cd7 100644 --- a/src/_data/glossary.yml +++ b/src/_data/glossary.yml @@ -614,7 +614,7 @@ - text: "Covariance and contravariance" link: "https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)" - text: "The covariant keyword" - link: "/deprecated/sound-problems#the-covariant-keyword" + link: "/language/type-system#covariant-keyword" labels: - "language" - "type system" diff --git a/src/_data/keywords.yml b/src/_data/keywords.yml index 8dd174964b..eff57ee568 100644 --- a/src/_data/keywords.yml +++ b/src/_data/keywords.yml @@ -35,7 +35,7 @@ link: /language/loops#break-and-continue type: reserved - term: 'covariant' - link: /deprecated/sound-problems#the-covariant-keyword + link: /language/type-system#covariant-keyword type: bit - term: 'default' link: /language/branches#switch diff --git a/src/content/deprecated/sound-problems.md b/src/content/deprecated/sound-problems.md index 2e4b04e7b5..339d460f9c 100644 --- a/src/content/deprecated/sound-problems.md +++ b/src/content/deprecated/sound-problems.md @@ -257,7 +257,7 @@ For more information, see :::note If you have a valid reason to use a subtype, you can use the -[covariant keyword](#the-covariant-keyword). +[covariant keyword](/language/type-system#covariant-keyword). :::
@@ -600,35 +600,8 @@ assumeStrings(names.[!cast!]()); ### The covariant keyword -Some (rarely used) coding patterns rely on tightening a type -by overriding a parameter's type with a subtype, which is invalid. -In this case, you can use the `covariant` keyword to -tell the analyzer that you are doing this intentionally. -This removes the static error and instead checks for an invalid -argument type at runtime. - -The following shows how you might use `covariant`: - - -```dart tag=passes-sa -class Animal { - void chase(Animal x) { ... } -} - -class Mouse extends Animal { ... } - -class Cat extends Animal { - @override - void chase([!covariant!] Mouse x) { ... } -} -``` - -Although this example shows using `covariant` in the subtype, -the `covariant` keyword can be placed in either the superclass -or the subclass method. -Usually the superclass method is the best place to put it. -The `covariant` keyword applies to a single parameter and is -also supported on setters and fields. +The documentation on the `covariant` keyword has been +moved to [The Dart Type system](/language/type-system#covariant-keyword). [bottom type]: https://en.wikipedia.org/wiki/Bottom_type [cast()]: {{site.dart-api}}/dart-core/Iterable/cast.html diff --git a/src/content/language/extend.md b/src/content/language/extend.md index 575af4a4ea..782067ebfa 100644 --- a/src/content/language/extend.md +++ b/src/content/language/extend.md @@ -78,7 +78,7 @@ it's similar to a downcast in that it can cause a type error at runtime. Still, narrowing the type is possible if the code can guarantee that a type error won't occur. In this case, you can use the -[`covariant` keyword](/deprecated/sound-problems#the-covariant-keyword) +[`covariant` keyword](/language/type-system#covariant-keyword) in a parameter declaration. For details, see the [Dart language specification][]. diff --git a/src/content/language/type-system.md b/src/content/language/type-system.md index d3613dd6e8..511499cab7 100644 --- a/src/content/language/type-system.md +++ b/src/content/language/type-system.md @@ -179,7 +179,7 @@ subtype of the original parameter. :::note If you have a valid reason to use a subtype, you can use the -[`covariant` keyword](/deprecated/sound-problems#the-covariant-keyword). +[`covariant` keyword](/language/type-system#covariant-keyword). ::: Consider the `chase(Animal)` method for the `Animal` class: @@ -483,6 +483,38 @@ For more information, see [Use sound return types when overriding methods](#use-proper-return-types) and [Use sound parameter types when overriding methods](#use-proper-param-types). + +#### Covariant parameters + +Some (rarely used) coding patterns rely on tightening a type +by overriding a parameter's type with a subtype, which is invalid. +In this case, you can use the `covariant` keyword to +tell the analyzer that you're doing this intentionally. +This removes the static error and instead checks for an invalid +argument type at runtime. + +The following shows how you might use `covariant`: + + +```dart tag=passes-sa +class Animal { + void chase(Animal x) { ... } +} + +class Mouse extends Animal { ... } + +class Cat extends Animal { + @override + void chase([!covariant!] Mouse x) { ... } +} +``` + +Although this example shows using `covariant` in the subtype, +the `covariant` keyword can be placed in either the superclass +or the subclass method. +Usually the superclass method is the best place to put it. +The `covariant` keyword applies to a single parameter and is +also supported on setters and fields. ## Other resources