Skip to content
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

Generics terminology #447

Merged
merged 21 commits into from
May 10, 2021
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ba0eb91
Creating new proposal: Generics terminology
josh11b Apr 10, 2021
1fe25bf
Filling out template with PR 447
josh11b Apr 10, 2021
dc35496
Fill in terminology.md, authored in PR #36.
josh11b Apr 12, 2021
b2c1cb3
Merge remote-tracking branch 'origin/generics-terminology' into gener…
josh11b Apr 12, 2021
e4cd1bb
Update link
josh11b Apr 12, 2021
e57527d
Delete stray temp file.
josh11b Apr 13, 2021
7b9bd08
Proposals now include a rationale.
josh11b Apr 13, 2021
d867188
Implement suggestion.
josh11b Apr 16, 2021
27d5198
Markdown instead of HTML strikethrough.
josh11b Apr 16, 2021
baa2ea4
Apply suggestions from code review
josh11b Apr 22, 2021
329e638
Merge remote-tracking branch 'upstream/trunk' into generics-terminology
josh11b Apr 24, 2021
177f8e8
Fix pre-commit.
josh11b Apr 24, 2021
26acffa
Wikipedia link to structural versus nominal.
josh11b Apr 26, 2021
c163b26
Switch from subsumption terminology to subtyping.
josh11b Apr 29, 2021
6e6211e
Implement suggestions. Remove `:` from vars & params.
josh11b Apr 30, 2021
4f56d67
Use suggestion.
josh11b Apr 30, 2021
36cce7b
Fix wrong definition of covariance/contravariance.
josh11b May 3, 2021
67868be
Merge remote-tracking branch 'upstream/trunk' into generics-terminology
josh11b May 4, 2021
91a4174
Update "Generics goals" to reference terminology doc.
josh11b May 4, 2021
51c5ca9
Merge remote-tracking branch 'upstream/trunk' into generics-terminology
josh11b May 10, 2021
a611bb2
Try to adjust text to address comments.
josh11b May 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions docs/design/generics/terminology.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- [Nominal interfaces](#nominal-interfaces)
- [Impls: Implementations of interfaces](#impls-implementations-of-interfaces)
- [Compatible types](#compatible-types)
- [Subsumption and casting](#subsumption-and-casting)
- [Subtyping and casting](#subtyping-and-casting)
- [Adapting a type](#adapting-a-type)
- [Type erasure](#type-erasure)
- [Facet type](#facet-type)
Expand Down Expand Up @@ -311,14 +311,36 @@ invariants, such as implementing the API of the new type by calling (public)
methods of the original API, instead of accessing any private implementation
details.

## Subsumption and casting

Both subsumption and casting are different names for changing the type of a
value to a compatible type.

Subsumption is an automatic or implicit conversion that happens to argument
values when calling a function or when assigning a value to a variable of a
different type.
## Subtyping and casting

Both subtyping and casting are different names for changing the type of a value
to a compatible type.

[Subtyping](https://en.wikipedia.org/wiki/Subtyping) is a relationship between
two types where you can safely operate on a value of one type using a variable
of another. For example, using C++'s object-oriented features, you can operate
on a value of a derived class using a pointer to the base class.
[Contravariance](<https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)>)
means you can pass a more specific type to a function that can handle a more
general type.
[Covariance](<https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)>)
means a function can return a more specific type to a caller prepared to handle
a more general type.
zygoloid marked this conversation as resolved.
Show resolved Hide resolved

In a generics context, we are specifically interested in the subtyping
relationships between [type-types](#type-type). In particular, a type-type
encompasses a set of [type constraints](#type-constraints), and you can convert
a type from a more-restrictive type-type to another type-type whose constraints
are implied by the first. C++ concepts terminology uses the term
["subsumes"](https://en.cppreference.com/w/cpp/language/constraints#Partial_ordering_of_constraints)
to talk about this partial ordering of constraints, but we avoid that term since
it is at odds with the use of the term in
[object-oriented subtyping terminology](https://en.wikipedia.org/wiki/Subtyping#Subsumption).

Note that subtyping is a bit like
[coercion](https://en.wikipedia.org/wiki/Type_conversion), except we want to
make it clear that the data representation of the value is not changing, just
its type as reflected in the API available to manipulate the value.

Casting is indicated explicitly by way of some syntax in the source code. You
might use a cast to switch between [type adaptations](#adapting-a-type), or to
Expand All @@ -327,11 +349,6 @@ be explicit where an implicit cast would otherwise occur. For now, we are saying
type `y`. Note that outside of generics, the term "casting" includes any
explicit type change, including those that change the data representation.

Note that subsumption is a bit like
[coercion](https://en.wikipedia.org/wiki/Type_conversion), except we want to
make it clear that the data representation of the value is not changing, just
its type as reflected in the API available to manipulate the value.

## Adapting a type

A type can be adapted by creating a new type that is
Expand All @@ -341,7 +358,7 @@ different implementations of the same interfaces.

Unlike extending a type (as with C++ class inheritance), you are not allowed to
add new data fields onto the end of the representation -- you may only change
the API. This means that it is safe to [cast](#subsumption-and-casting) a value
the API. This means that it is safe to [cast](#subtyping-and-casting) a value
between those two types without any dynamic checks or danger of
[object slicing](https://en.wikipedia.org/wiki/Object_slicing).

Expand Down