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

Proposal for Generics #36

Closed
wants to merge 320 commits into from
Closed
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
320 commits
Select commit Hold shift + click to select a range
a743050
Iterating on the requiring interfaces section.
josh11b Feb 11, 2021
5c88318
Interface extension, but still pretty rough.
josh11b Feb 11, 2021
36095f2
Add type compatibility section
josh11b Feb 11, 2021
0a308aa
Get rid of `TypeImplements` and implicit interface args.
josh11b Feb 12, 2021
037e2c8
Delete interface nesting/containment, fix refs.
josh11b Feb 12, 2021
85070ce
Add future work & todos, remove interface adapter.
josh11b Feb 12, 2021
59fd882
Resolve TODOs in interface extension.
josh11b Feb 12, 2021
f8c5cf1
Checkpoint progress.
josh11b Feb 12, 2021
cc2c9ae
Checkpoint progress.
josh11b Feb 22, 2021
4f39c55
Checkpoint progress.
josh11b Feb 23, 2021
bc4090e
Example of using associated constants.
josh11b Feb 23, 2021
0a022c0
Constraints on associated constants.
josh11b Feb 23, 2021
0696f19
Move associated constants after associated types.
josh11b Feb 24, 2021
f3ea89d
Consolidation of difficult constraints on associated constants & types.
josh11b Feb 24, 2021
5fa6788
Fix intro to associated types.
josh11b Feb 25, 2021
10bd8fc
Checkpoint progress.
josh11b Feb 25, 2021
53862cd
Resolve TODOs in adapting types section.
josh11b Feb 25, 2021
a09dee0
Checkpoint progress.
josh11b Feb 25, 2021
6df868e
Add comparisons to Rust and Swift for some features. Add tricky case …
josh11b Feb 25, 2021
30abc93
Add link to type constraints doc.
josh11b Feb 25, 2021
29ae6db
Add detail to type compatibility with adaptors.
josh11b Feb 25, 2021
961bdb2
Checkpoint progress.
josh11b Feb 25, 2021
278f3a3
Additional Swift/Rust context for GAT and Higher-ranked types
josh11b Feb 25, 2021
778d940
More links to other languages.
josh11b Feb 26, 2021
217c228
Move some content from "one construct for structural type constraints…
josh11b Feb 26, 2021
917bbae
Example showing how `Type` works.
josh11b Mar 1, 2021
04705a8
Checkpoint progress.
josh11b Mar 1, 2021
8696c8e
Add link to Rust generic traits.
josh11b Mar 2, 2021
461491e
Add "interface defaults" to future work.
josh11b Mar 3, 2021
19baf10
Switch `extends` syntax to support using associated types in constrai…
josh11b Mar 3, 2021
f042740
Checkpoint progress.
josh11b Mar 3, 2021
72980a3
Checkpoint progress.
josh11b Mar 3, 2021
900ee05
More on specialization.
josh11b Mar 3, 2021
7b362dc
Checkpoint progress.
josh11b Mar 3, 2021
08080a8
Checkpoint progress.
josh11b Mar 4, 2021
7dbd177
Checkpoint progress.
josh11b Mar 4, 2021
055b09d
Split comparison of alternatives out of overview.
josh11b Mar 4, 2021
3ce770b
Checkpoint progress.
josh11b Mar 4, 2021
e389ebc
Rust links. Abstract return types. Fleshing out overview.
josh11b Mar 5, 2021
a2f9c9f
Switch to associated items terminology, reorganize associated constan…
josh11b Mar 5, 2021
50a2de0
Move text about constraints into its own section. Still needs to be t…
josh11b Mar 5, 2021
66ce2e3
Switch from two kinds of interface parameters to just multi.
josh11b Mar 5, 2021
822bf0f
Checkpoint progress.
josh11b Mar 5, 2021
8b38d0b
Update "structural confromance" section of templated impls.
josh11b Mar 6, 2021
a5f108a
Organize existing text about constraints, still needs lots of rewriting.
josh11b Mar 7, 2021
4787f14
Only have `multi` interface parameters now.
josh11b Mar 7, 2021
f9ad6bb
`CompatibleWith` only needs one parameter now.
josh11b Mar 7, 2021
e629174
Should `Sized` types expose a `ByteSize` associated constant?
josh11b Mar 7, 2021
a63a347
Clarify that the constraint approach is still an open question.
josh11b Mar 7, 2021
b36bb3a
Link to Swift generic type equality is undecidable.
josh11b Mar 7, 2021
82de3df
Explain the essential difference between the constraint approaches.
josh11b Mar 7, 2021
d9deeb3
Checkpoint progress.
josh11b Mar 8, 2021
6b03556
Checkpoint progress.
josh11b Mar 8, 2021
88d553d
Checkpoint progress.
josh11b Mar 8, 2021
4872a47
Checkpoint progress.
josh11b Mar 8, 2021
789654d
Add "evolution" and "testing" as future work.
josh11b Mar 8, 2021
8c5b1ab
Checkpoint progress.
josh11b Mar 8, 2021
fea61b7
Add canonical type algorithm for generic type equality with argument …
josh11b Mar 9, 2021
75dfbaf
Add a conclusion to constraints listing options.
josh11b Mar 9, 2021
4436634
Checkpoint progress.
josh11b Mar 10, 2021
cebf756
Recursive constraints using `.Self`.
josh11b Mar 10, 2021
16ce490
Progress on constraints.
josh11b Mar 11, 2021
f7021d2
Get range constraints into okay shape.
josh11b Mar 11, 2021
efc499e
More work on constraints.
josh11b Mar 11, 2021
594bf57
`NTuple` example for interface parameters.
josh11b Mar 11, 2021
c7a5c6e
Open question about generic type equality
josh11b Mar 11, 2021
a3d8f84
Make `extends` example a little less generic.
josh11b Mar 11, 2021
0e1d02d
More work on constraints.
josh11b Mar 12, 2021
fa9c45d
Fill in "is a subtype" section of constraints.
josh11b Mar 12, 2021
23abe58
Checkpoint progress.
josh11b Mar 12, 2021
0e9d835
Default implementation of interface.
josh11b Mar 12, 2021
f480c53
Covariant refinement of associated types.
josh11b Mar 12, 2021
339e868
Implicit/inferred constraints and associated types.
josh11b Mar 12, 2021
fc3c1a9
Remove "index of examples" since its redundant with "future work".
josh11b Mar 12, 2021
0d84f8a
Delete some old notes that have been addressed.
josh11b Mar 13, 2021
cf7e019
`A & B` future work, refining type bounds is in interface extension, …
josh11b Mar 15, 2021
40d3ea0
Move some notes at the end to their relevant section.
josh11b Mar 15, 2021
6f72498
Move a note about field requirements.
josh11b Mar 15, 2021
eadfda4
Merge remote-tracking branch 'upstream/trunk' into generics-docs
josh11b Mar 19, 2021
b5dca03
Formatting into sections.
josh11b Mar 19, 2021
87da469
Add an idea for evolution.
josh11b Mar 19, 2021
4460e82
Finish fixing doc boilerplate.
josh11b Mar 19, 2021
9046bef
Small changes.
josh11b Mar 19, 2021
34783be
Fix pre-commit.
josh11b Mar 19, 2021
a0c7622
More vs -> versus
josh11b Mar 19, 2021
f820316
Fix links, particularly anchors.
josh11b Mar 19, 2021
4b9eedc
Fix double TOC heading.
josh11b Mar 19, 2021
de2ae7e
Lots of progress on constraints.
josh11b Mar 24, 2021
6696e02
Include constraint on recursion with where clauses.
josh11b Mar 25, 2021
4e74b3f
Checkpoint progress.
josh11b Mar 25, 2021
17f4c56
Checkpoint progress.
josh11b Mar 25, 2021
9c81e7d
Question: infer associated types?
josh11b Mar 25, 2021
32a1106
Reorganize table.
josh11b Mar 26, 2021
7296c4a
Clarify rule.
josh11b Mar 26, 2021
b57ffaa
Clarify second chart.
josh11b Mar 26, 2021
1c5bd8b
Reoderd tables.
josh11b Mar 26, 2021
e0fe955
Checkpoint progress.
josh11b Mar 26, 2021
591c295
Checkpoint progress.
josh11b Mar 26, 2021
ba70a94
Swap columns back
josh11b Mar 26, 2021
72b9ac2
Checkpoint progress.
josh11b Mar 26, 2021
6dae7d4
Analogy between interfaces and structural interfaces.
josh11b Mar 27, 2021
b90e1cc
Add to interface evolution
josh11b Mar 27, 2021
0d09f53
More text about implementing structural interfaces.
josh11b Mar 27, 2021
878e6bf
More on generic type equality.
josh11b Mar 28, 2021
5c7a5b9
Merge remote-tracking branch 'upstream/trunk' into generics-docs
josh11b Mar 29, 2021
d398495
Fill in details in "Type equality with argument passing".
josh11b Mar 30, 2021
336b798
More about type equality with where clauses
josh11b Mar 31, 2021
3a3c525
Links to Swift GAT, etc.
josh11b Mar 31, 2021
0408624
Future work: Generic type specialization
josh11b Apr 1, 2021
fa92f20
Add use case for adaptors re: mixing independent libraries.
josh11b Apr 1, 2021
0f98707
Link to swift inferred constraints.
josh11b Apr 1, 2021
a13d055
Swift considered removing associated type inference.
josh11b Apr 3, 2021
160967a
Comparison with Rust type parameters, "input"/"output" terminology.
josh11b Apr 3, 2021
5cf6bf4
Syntax for adapting a type while preserving its API.
josh11b Apr 3, 2021
db80962
More on defaults.
josh11b Apr 5, 2021
f8b598d
Splitting terminology up, step 1.
josh11b Apr 7, 2021
6f90c00
More work on terminology.
josh11b Apr 8, 2021
bae2655
Refine specialization.
josh11b Apr 8, 2021
7d3d98e
Nominal and structural interfaces have their own sections now.
josh11b Apr 8, 2021
86e848b
Merge remote-tracking branch 'upstream/trunk' into generics-docs
josh11b Apr 12, 2021
6d4c605
Include PR in README.
josh11b Apr 12, 2021
d930856
Merge branch 'generics-docs' of github.com:josh11b/carbon-lang into g…
josh11b Apr 12, 2021
57bcba1
Arguments -> parameters, and fix links.
josh11b Apr 12, 2021
3071af2
`adaptor` -> `adapter`
josh11b Apr 20, 2021
88d55c3
Change syntax for `extends` in an `adapter`.
josh11b Apr 20, 2021
5d5f7e8
argument -> parameter
josh11b Apr 22, 2021
dd61929
Merge remote-tracking branch 'upstream/trunk' into generics-docs
josh11b Apr 24, 2021
d4ca36b
Link to future work from the goals.
josh11b Apr 24, 2021
6821916
Name conflicts when implementing 2+ interfaces inline.
josh11b Apr 25, 2021
356d374
Future work: reverse generics"
josh11b Apr 26, 2021
e16447c
Define `auto`.
josh11b Apr 26, 2021
6b828ec
More about Swift generic syntax under consideration.
josh11b Apr 26, 2021
fc59658
Reference discussions about adding `any Protocol` to Swift.
josh11b Apr 26, 2021
3809319
Add anchor to link re: Rust where motivation.
josh11b Apr 29, 2021
e6484c4
Update subsumption->subtyping in links.
josh11b Apr 29, 2021
0d140d4
Support other newtype use cases with adapters.
josh11b May 2, 2021
66255cd
Record a disadvantage of the argument passing constraint approach.
josh11b May 5, 2021
863e993
Merge remote-tracking branch 'upstream/trunk' into generics-docs
josh11b May 5, 2021
412384a
Add links to object safety. Most of an edit pass on overview.
josh11b May 6, 2021
8fe9ce5
Improve syntax examples.
josh11b May 6, 2021
99a55ba
Improve bits about structural interfaces.
josh11b May 6, 2021
b8c0813
Use `is` instead of `as` in `where` clauses.
josh11b May 6, 2021
b50464d
Clarify specialization and instantiation.
josh11b May 6, 2021
2a6a5d4
Checkpoint progress.
josh11b May 7, 2021
bc89d1f
Covariant refinement of associated types is a case of constraints.
josh11b May 7, 2021
17561f5
Merge remote-tracking branch 'upstream/trunk' into generics-docs
josh11b May 12, 2021
3af60e5
Rename combined-interfaces -> details
josh11b May 14, 2021
8f4b5c3
Scrub links
josh11b May 14, 2021
16427d5
Editing pass up to & including "Structural interface" section.
josh11b May 17, 2021
b0964e1
`+` -> `&` following feedback on #531.
josh11b May 17, 2021
f3d516e
Better names in some examples.
josh11b May 19, 2021
6a6ab1f
#531 has now been resolved.
josh11b May 19, 2021
901789a
TODO: clarify structural interfaces vs. blanket impls
josh11b May 19, 2021
c7e9e93
Use better names in examples.
josh11b May 20, 2021
b093ea2
Improve interface extension section.
josh11b May 22, 2021
5bd4bc2
Move "Covariant refinement" section to "constraints" & revise.
josh11b May 24, 2021
d42c4b8
Realistic example of diamond dependency. Section for extends in struc…
josh11b May 24, 2021
7434b58
Clean up "extends, impl with structural interfaces" section.
josh11b May 24, 2021
66d70ae
Clean up overload resolution section.
josh11b May 24, 2021
192f2e8
Checkpoint progress.
josh11b May 25, 2021
c29718b
Continued work on adapters.
josh11b May 25, 2021
8361547
Update adapter use case.
josh11b May 25, 2021
13ea5c8
Finish updating adapter section.
josh11b May 25, 2021
97d3ce2
Merge remote-tracking branch 'upstream/trunk' into generics-docs
josh11b May 25, 2021
003f874
Changes from forking details.md in #553.
josh11b May 26, 2021
a193796
Add FIXME for content deleted from #553.
josh11b May 26, 2021
a2f0ae9
Mark template content that isn't in #524.
josh11b May 26, 2021
cbf3bd0
Improve associated constant example.
josh11b May 27, 2021
3dadcae
Advantage of inferring associated types
josh11b May 27, 2021
ec9ef5d
Fix missing $
josh11b May 27, 2021
0572950
Update to latest syntax, plus minor fixes.
josh11b May 27, 2021
4e15c40
Suggestion to rename `extends` to `provides` from Wolff.
josh11b Jun 1, 2021
2184ee6
Checkpoint progress.
josh11b Jun 7, 2021
a8769ad
New template->generic evolution future work section.
josh11b Jun 8, 2021
7969e9b
Add link to generics-to-template doc to evolution section.
josh11b Jun 8, 2021
8d3192d
Add `auto` idea to implicit constraints.
josh11b Jun 9, 2021
2cf1478
Add future work on variadics
josh11b Jun 10, 2021
0f0255d
`CompatibleWith` and `FacetOf` define equivalence relations on types.
josh11b Jun 10, 2021
b3aaaf4
Widget -> Song
josh11b Jun 10, 2021
90a7525
Future work for inheritance interaction.
josh11b Jun 11, 2021
b279d04
Move content around.
josh11b Jun 21, 2021
d2176f6
Move more content.
josh11b Jun 21, 2021
b09ab62
Implement decision on #575 re: impl syntax, and other fixes I noticed.
josh11b Jun 23, 2021
49f4a54
Add `TypeId` interface.
josh11b Jun 23, 2021
de23178
Fixes to `MaybeBoxed`.
josh11b Jun 23, 2021
95ee2e1
Checkpoint progress.
josh11b Jun 24, 2021
e025231
Checkpoint progress.
josh11b Jun 25, 2021
4db8530
Checkpoint progress.
josh11b Jun 28, 2021
457603c
Checkpoint progress.
josh11b Jun 28, 2021
20bddc1
Make an appendix for deduced interface params
josh11b Jun 29, 2021
d76b14e
Update terminology
josh11b Jun 29, 2021
dc18117
Improve conditional conformane and parameterized impls
josh11b Jun 30, 2021
665f176
Improve associated constants and parameterized interfaces
josh11b Jun 30, 2021
0817d62
No operator methods like C++
josh11b Jun 30, 2021
89b39dd
Move rejected impl syntax to appendix
josh11b Jun 30, 2021
a13c54e
Move rejected alternatives to appendix
josh11b Jul 1, 2021
d7c66c4
Checkpoint progress.
josh11b Jul 1, 2021
7775fa6
Other dynamic types
josh11b Jul 1, 2021
789a8f5
Mention CoerceUnsized
josh11b Jul 2, 2021
00a5602
Add type-of-type parameter to CompatibleWith
josh11b Jul 2, 2021
63bbfe6
Checkpoint progress.
josh11b Jul 2, 2021
37f4cd7
Open problems with specialization.
josh11b Jul 2, 2021
849cfe3
Access to base type in adapters
josh11b Jul 3, 2021
ce9793b
Implement #565, `:$` -> `:!`, `:$$` -> `template`
josh11b Jul 9, 2021
049444e
Update method syntax to match #494
josh11b Jul 9, 2021
3e36ed8
`final` methods in interfaces
josh11b Jul 22, 2021
777df4a
Work on conditional conformance & parameterized impls
josh11b Jul 30, 2021
bc55f3f
Fix `where` clauses
josh11b Jul 30, 2021
7e88870
Fix earlier conversion error
josh11b Jul 30, 2021
e9bd022
`var` -> `let` for associated constants/types
josh11b Jul 30, 2021
6034955
`alias` -> `let` for named constraints and associated constants
josh11b Jul 30, 2021
a48ec95
struct -> class to reflect decision on #651.
josh11b Jul 30, 2021
dfce1d9
More edits through parameterized interfaces
josh11b Aug 3, 2021
2c683ac
Fix bad syntax rewrite
josh11b Aug 4, 2021
bfd002f
Replace value pattern with `type_of`
josh11b Aug 5, 2021
5a19bad
Specialization perf is fragile in Rust
josh11b Aug 11, 2021
26cd841
let :!
josh11b Aug 11, 2021
28a3f57
Checkpoint progress.
josh11b Aug 18, 2021
8e5873a
Checkpoint progress.
josh11b Aug 18, 2021
cd51b87
Checkpoint progress.
josh11b Aug 19, 2021
d6b6831
Fix syntax
josh11b Aug 20, 2021
3f5b5da
Checkpoint progress.
josh11b Aug 20, 2021
7883972
Fixes
josh11b Aug 21, 2021
f4c2fe9
Fix
josh11b Aug 21, 2021
75dd84e
New Swift variadics pitch
josh11b Aug 30, 2021
b780590
Progress on new constraints
josh11b Sep 2, 2021
a80429f
Checkpoint progress.
josh11b Sep 3, 2021
31f9d23
Checkpoint progress.
josh11b Sep 3, 2021
c229621
Checkpoint progress.
josh11b Sep 4, 2021
714dea0
InterfaceType -> ConstraintType
josh11b Sep 15, 2021
005114f
Do blanket impls satisfy requirements? More on defaults
josh11b Sep 18, 2021
ced0ce2
Links
josh11b Oct 8, 2021
fc25029
Add links
josh11b Oct 11, 2021
24a5fdc
Merge abstract return types and reverse generics
josh11b Oct 11, 2021
e3a363e
Archive dead ends
josh11b Oct 12, 2021
6a09d27
Save some text deleted from the current proposal
josh11b Oct 12, 2021
e303903
Save content deleted from the proposal
josh11b Oct 12, 2021
dabade4
Add link re: abstract return types
josh11b Oct 13, 2021
685567a
Checkpoint progress.
josh11b Oct 15, 2021
56e1bb9
Parameterized impls use cases and prioritization
josh11b Oct 20, 2021
ce61fba
Iterating on specialization
josh11b Oct 20, 2021
09e5e3f
Checkpoint progress.
josh11b Oct 21, 2021
0c6a092
Checkpoint progress.
josh11b Oct 25, 2021
1626f62
Checkpoint progress.
josh11b Oct 27, 2021
49e7c78
Checkpoint progress.
josh11b Oct 28, 2021
a8f32e5
Checkpoint progress.
josh11b Oct 29, 2021
b54d842
Checkpoint progress.
josh11b Nov 1, 2021
954ff1b
Link to Swift "any" pitch
josh11b Nov 30, 2021
f91c167
Add link to Swift generics
josh11b Feb 8, 2022
0a1664d
Expand variadics example
josh11b Feb 8, 2022
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
1 change: 1 addition & 0 deletions .codespell_ignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
circularly
copyable
pullrequest
compiletime
josh11b marked this conversation as resolved.
Show resolved Hide resolved
119 changes: 119 additions & 0 deletions docs/design/generics/appendix-interface-param-impl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<!--
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
Exceptions. See /LICENSE for license information.
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-->

# Carbon: Separate implementation of parameterized interfaces problem

## Background

Interface implementation is Carbon's only language construct that allows open
extension, and this sort of open extension is needed to address the "expression
problem" in programming language design. However, we need to limit which
libraries can implement an interface for a type so we can be guaranteed to see
the implementation when we try and use it.

## Question

Can we allow an implementation of a parameterized interface `I(T)` for a type
`A` to be in the same library as `T`, or can it only be provided with `I` or
`A`?

## Answer

It can only be provided with `I` or `A`, unless `T` can not be deduced, and then
it could be provided with `T` as well. Recall for interfaces where we want to
allow the same interface to be implemented for a type with two different
parameters, we can't deduce `T`. For example, we may want to support
`ComparableTo(Float32)` and `ComparableTo(Complex128)` for type `Complex64`. So
the argument to `ComparableTo` can't be deduced and it wouldn't break anything
to allow those implementations to be defined with the argument (`Float32` or
`Complex128`).

## Problem

Consider this collection of libraries, where there are implementations for an
interface `I(T)` for a type `A`, and those implementations are in the libraries
defining the type parameter:

```
package X library "I and A" api;

interface I(Type:$ T) { ... }

struct A { ... }
```

```
package Y library "T1" api;

import X library "I and A";

struct T1 { ... }

// Type `X.A` has an implementation for `X.I(T)` for `T == Y.T1`.
impl X.I(T1) for X.A { ... }
```

```
package Z library "T2" api;

import X library "I and A";

struct T2 { ... }

// Type `X.A` has an implementation for `X.I(T)` for `T == Z.T2`.
impl X.I(T2) for X.A { ... }
```

```
package Main api;

import X library "I and A";
// Consider what happens if we include different combinations
// of the following two statements:
// import Y library "T1";
// import Z library "T2";

// Function `F` is called with value `a` with type `U`,
// where `U` implements interface `X.I(T)` for some type `T`.
fn F[Type:$ T, X.I(T):$ U](U:$ a) { ... }

fn Main() {
var X.A: a = X.A.Init();
F(a);
}
```

(I have placed the libraries in different packages, but the packages are not the
important part here.)

The `F(a)` call triggers a lookup for implementations of the interface `X.I(T)`
for some `T`. There exists such implementations in both libraries `Y.T1` and
`Z.T2` for different values of `T`. This has a number of sad consequences:

- "Import what you use" is hard to measure: libraries `Y.T1` and `Z.T2` are
important/used even though `Y` and `Z` are not mentioned outside the
`import` statement.
- The call `F(a)` has different interpretations depending on what libraries
are imported:
- If neither is imported, it is an error.
- If both are imported, it is ambiguous.
- If only one is imported, you get totally different code executed
depending on which it is.
- We have no way of enforcing a "one implementation per interface" rule that
would prevent the call to `F` from being ambiguous.

Basically, there is nothing guaranteeing that we import libraries defining the
types that are used as interface parameters if we allow the interface parameters
to be deduced. For cases where we do _not_ allow interface parameters to be
deduced, because we want to allow multiple implementations of that interface for
a single type (such as `ComparableTo(T)`), this problem doesn't arise.

## Conclusion

It appears we need to require all implementations of interface `I(...)` for type
`A(...)` to live in the same library as either the definition of `I`, `A`, or a
parameter of `A`. Being in the same library as a parameter of `I` is
insufficient, unless it can't be deduced.
Loading