Skip to content

Commit

Permalink
Move covariant keyword docs out of sound problems page (#6299)
Browse files Browse the repository at this point in the history
Contributes to #6265
  • Loading branch information
parlough authored Jan 4, 2025
1 parent 24fd674 commit 10d79c3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 35 deletions.
2 changes: 1 addition & 1 deletion firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion src/_data/glossary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/_data/keywords.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 3 additions & 30 deletions src/content/deprecated/sound-problems.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
:::

<hr>
Expand Down Expand Up @@ -600,35 +600,8 @@ assumeStrings(names.[!cast!]<String>());

### 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`:

<?code-excerpt "lib/covariant.dart" replace="/covariant/[!$&!]/g"?>
```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
Expand Down
2 changes: 1 addition & 1 deletion src/content/language/extend.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][].
Expand Down
34 changes: 33 additions & 1 deletion src/content/language/type-system.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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).

<a id="covariant-keyword" aria-hidden="true"></a>
#### 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`:

<?code-excerpt "lib/covariant.dart" replace="/covariant/[!$&!]/g"?>
```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

Expand Down

0 comments on commit 10d79c3

Please sign in to comment.