-
Notifications
You must be signed in to change notification settings - Fork 723
Glossary entries for "Subtype" and "Subclass" #4958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2bab70e
attempt to define subclass/subtype
MaryaBelanger 416215a
type system link
MaryaBelanger 4a992fd
Merge branch 'main' into subtype/subclass
MaryaBelanger 229489e
Merge branch 'main' into subtype/subclass
MaryaBelanger de93a17
Merge branch 'main' into subtype/subclass
MaryaBelanger f781c39
Merge branch 'main' into subtype/subclass
MaryaBelanger 26f0ad9
Merge branch 'main' into subtype/subclass
atsansone 9502ff9
Merge branch 'main' into subtype/subclass
parlough 8228034
Merge branch 'main' into subtype/subclass
MaryaBelanger dddf51a
rewrite from feedback
MaryaBelanger bf09daf
apply fishythefish review
MaryaBelanger 9a6bc6b
remove solely
MaryaBelanger 344c828
fix some passive voice, paragraph
MaryaBelanger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -279,3 +279,60 @@ If not, the pattern _refutes_, or denies, the match. | |
Refutable patterns appear in [_matching contexts_][]. | ||
|
||
[_matching contexts_]: /language/patterns#matching | ||
|
||
## Subclass | ||
|
||
A _subclass_ is a class that inherits the implementation of another class by using the | ||
[`extends`](/language/extend) keyword, or by [mixin application](#mixin-application). | ||
|
||
```dart | ||
class A extends B {} // A is a subclass of B; B is the superclass of A. | ||
|
||
class B1 extends A with M {} // B1 has the superclass `A with M`, which has the superclass A. | ||
``` | ||
|
||
A subclass relation also implies an associated [subtype](#subtype) relation. | ||
For example, `class A` implicitly defines an associated type `A` | ||
which instances of the class `A` inhabit. | ||
So, `class A extends B` declares not just that the class | ||
`A` is a subclass of `B`, but also establishes that the *type* `A` is a | ||
*subtype* of the type `B`. | ||
|
||
Subclass relations are a subset of subtype relations. | ||
When the documentation says "`S` must be a subtype of `T`", | ||
it's fine for `S` to be a subclass of `T`. | ||
However, the converse is not true: not all subtypes are subclasses. | ||
See the [subtype](#subtype) entry for more information. | ||
|
||
## Subtype | ||
|
||
A _subtype_ relation is where a value of a certain type is substitutable | ||
where the value of another type, the supertype, is expected. | ||
For example, if `S` is a subtype of `T`, | ||
then you can substitute a value of type `S` | ||
where a value of type `T` is expected. | ||
|
||
A subtype supports all of the operations of its supertype | ||
(and possibly some extra operations). | ||
In practice, this means you can assign the value of a subtype | ||
to any location expecting the supertype, | ||
and all of the methods of the supertype are available on the subtype. | ||
|
||
This is true at least statically. | ||
A specific API might not allow the substitution at run time, | ||
depending on its operations. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it helps to have a specific example: void f(List<num> l) {
l.add(3.14);
}
|
||
|
||
Some subtype relations are based on the structure of the type, | ||
like with nullable types (for example, `int` is a subtype of `int?`) | ||
and function types | ||
(for example, `String Function()` is a subtype of `void Function()`). | ||
fishythefish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Subtypes can also be introduced for classes by | ||
[implementation](/language/classes#implicit-interfaces) | ||
or [inheritance](/language/extend) (direct or indirect): | ||
|
||
```dart | ||
class A implements B {} // A is a subtype of B, but NOT a subclass of B. | ||
|
||
class C extends D {} // C is a subtype AND a subclass of D. | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.