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
Show file tree
Hide file tree
Changes from 19 commits
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
3 changes: 2 additions & 1 deletion docs/design/generics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ feature of Carbon:
- ~~Overview~~ - not implemented yet
- [Goals](goals.md) - The motivation and principles guiding the design
direction.
- ~~Terminology~~ - not implemented yet
- [Terminology](terminology.md) - A glossary establishing common terminology
for describing the design.
- ~~Detailed design~~ - not implemented yet
- ~~Rejected alternatives~~ - not implemented yet
48 changes: 18 additions & 30 deletions docs/design/generics/goals.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

- [Purpose of this document](#purpose-of-this-document)
- [Background](#background)
- [Definition of generics](#definition-of-generics)
- [Generic parameters](#generic-parameters)
- [Interfaces](#interfaces)
- [Relationship to templates](#relationship-to-templates)
Expand Down Expand Up @@ -52,36 +51,25 @@ forward-looking.

## Background

### Definition of generics
Carbon will support
[generics](terminology.md#generic-versus-template-parameters) to support generic
programming by way of
[parameterization of language constructs](terminology.md#parameterized-language-constructs)
with [early type checking](terminology.md#early-versus-late-type-checking) and
[complete definition checking](terminology.md#complete-definition-checking).

**TODO** This should be replaced with a link to a terminology doc, once that is
accepted.

C++ supports
[parametric polymorphism](https://en.wikipedia.org/wiki/Parametric_polymorphism)
and [generic programming](https://en.wikipedia.org/wiki/Generic_programming)
through templates. Templates use
This is in contrast with the
[compile-time duck typing](https://en.wikipedia.org/wiki/Duck_typing#Templates_or_generic_types)
to decide whether arguments are valid. This is a form of
[structural typing](https://en.wikipedia.org/wiki/Structural_type_system) that
is usage based.

Carbon will support _generics_ in order to allow definition checking in generic
programming. Definition checking is accomplished by using
[bounded parametric polymorphism](https://en.wikipedia.org/wiki/Parametric_polymorphism#Bounded_parametric_polymorphism)
instead of compile-time duck typing. This means the legal arguments and the
legal uses of a parameter are both goverened by explicit bounds on the parameter
in a generic function's signature.

This would be _in addition_ to
approach of C++ templates, and _in addition_ to
[template support in Carbon](#relationship-to-templates), if we decide to
support templates in Carbon beyond interoperability with C++ templates.

### Generic parameters

Generic functions and generic types will all take some "generic parameters",
which will frequently be types, and in some cases will be implicit or inferred
from the types of the values of explicit parameters.
which will frequently be types, and in some cases will be
[implicit or inferred](terminology.md#implicit-parameter) from the types of the
values of explicit parameters.

If a generic parameter is a type, the generic function's signature can specify
constraints that the caller's type must satisfy. For example, a resizable array
Expand All @@ -97,13 +85,13 @@ the same type.

We need some way to express the constraints on a generic type parameter. In
Carbon we express these "type constraints" by saying we restrict to types that
implement specific _interfaces_. Interfaces describe an API a type could
implement; for example, it might specify a set of functions, including names and
signatures. A type implementing an interface may be passed as a generic type
argument to a function that has that interface as a requirement of its generic
type parameter. Then, the functions defined in the interface may be called in
the body of the function. Further, interfaces have names that allow them to be
reused.
implement specific [_interfaces_](terminology.md#interface). Interfaces describe
an API a type could implement; for example, it might specify a set of functions,
including names and signatures. A type implementing an interface may be passed
as a generic type argument to a function that has that interface as a
requirement of its generic type parameter. Then, the functions defined in the
interface may be called in the body of the function. Further, interfaces have
names that allow them to be reused.

Similar compile-time and run-time constructs may be found in other programming
languages:
Expand Down
Loading