Skip to content

Commit fbb3ec6

Browse files
committed
Associated types: Break up text for readability
1 parent 99a23f8 commit fbb3ec6

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

src/traits/associated-types.md

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ which we will introduce one by one:
1414
When a trait defines an associated type (e.g.,
1515
[the `Item` type in the `IntoIterator` trait][intoiter-item]), that
1616
type can be referenced by the user using an **associated type
17-
projection** like `<Option<u32> as IntoIterator>::Item`. (Often,
18-
though, people will use the shorthand syntax `T::Item` – presently,
19-
that syntax is expanded during
20-
["type collection"](../type-checking.html) into the explicit form,
21-
though that is something we may want to change in the future.)
17+
projection** like `<Option<u32> as IntoIterator>::Item`.
18+
19+
> Often, people will use the shorthand syntax `T::Item`. Presently, that
20+
> syntax is expanded during ["type collection"](../type-checking.html) into the
21+
> explicit form, though that is something we may want to change in the future.
2222
2323
[intoiter-item]: https://doc.rust-lang.org/nightly/core/iter/trait.IntoIterator.html#associatedtype.Item
2424

@@ -41,10 +41,11 @@ IntoIterator>::Item` to just `u32`.
4141

4242
In this case, the projection was a "monomorphic" one – that is, it
4343
did not have any type parameters. Monomorphic projections are special
44-
because they can **always** be fully normalized – but often we can
45-
normalize other associated type projections as well. For example,
46-
`<Option<?T> as IntoIterator>::Item` (where `?T` is an inference
47-
variable) can be normalized to just `?T`.
44+
because they can **always** be fully normalized.
45+
46+
Often, we can normalize other associated type projections as well. For
47+
example, `<Option<?T> as IntoIterator>::Item`, where `?T` is an inference
48+
variable, can be normalized to just `?T`.
4849

4950
In our logic, normalization is defined by a predicate
5051
`Normalize`. The `Normalize` clauses arise only from
@@ -60,9 +61,8 @@ forall<T> {
6061

6162
where in this case, the one `Implemented` condition is always true.
6263

63-
(An aside: since we do not permit quantification over traits, this is
64-
really more like a family of program clauses, one for each associated
65-
type.)
64+
> Since we do not permit quantification over traits, this is really more like
65+
> a family of program clauses, one for each associated type.
6666
6767
We could apply that rule to normalize either of the examples that
6868
we've seen so far.
@@ -76,17 +76,18 @@ normalized. For example, consider this function:
7676
fn foo<T: IntoIterator>(...) { ... }
7777
```
7878

79-
In this context, how would we normalize the type `T::Item`? Without
80-
knowing what `T` is, we can't really do so. To represent this case, we
81-
introduce a type called a **placeholder associated type
82-
projection**. This is written like so `(IntoIterator::Item)<T>`. You
83-
may note that it looks a lot like a regular type (e.g., `Option<T>`),
84-
except that the "name" of the type is `(IntoIterator::Item)`. This is
85-
not an accident: placeholder associated type projections work just like
86-
ordinary types like `Vec<T>` when it comes to unification. That is,
87-
they are only considered equal if (a) they are both references to the
88-
same associated type, like `IntoIterator::Item` and (b) their type
89-
arguments are equal.
79+
In this context, how would we normalize the type `T::Item`?
80+
81+
Without knowing what `T` is, we can't really do so. To represent this case,
82+
we introduce a type called a **placeholder associated type projection**. This
83+
is written like so: `(IntoIterator::Item)<T>`.
84+
85+
You may note that it looks a lot like a regular type (e.g., `Option<T>`),
86+
except that the "name" of the type is `(IntoIterator::Item)`. This is not an
87+
accident: placeholder associated type projections work just like ordinary
88+
types like `Vec<T>` when it comes to unification. That is, they are only
89+
considered equal if (a) they are both references to the same associated type,
90+
like `IntoIterator::Item` and (b) their type arguments are equal.
9091

9192
Placeholder associated types are never written directly by the user.
9293
They are used internally by the trait system only, as we will see
@@ -152,16 +153,16 @@ might just fail, in which case we get back `Err(NoSolution)`. This
152153
would happen, for example, if we tried to unify `u32` and `i32`.
153154

154155
The key point is that, on success, unification can also give back to
155-
us a set of subgoals that still remain to be proven (it can also give
156+
us a set of subgoals that still remain to be proven. (It can also give
156157
back region constraints, but those are not relevant here).
157158

158-
Whenever unification encounters an (un-placeholder!) associated type
159+
Whenever unification encounters a non-placeholder associated type
159160
projection P being equated with some other type T, it always succeeds,
160161
but it produces a subgoal `ProjectionEq(P = T)` that is propagated
161162
back up. Thus it falls to the ordinary workings of the trait system
162163
to process that constraint.
163164

164-
(If we unify two projections P1 and P2, then unification produces a
165-
variable X and asks us to prove that `ProjectionEq(P1 = X)` and
166-
`ProjectionEq(P2 = X)`. That used to be needed in an older system to
167-
prevent cycles; I rather doubt it still is. -nmatsakis)
165+
> If we unify two projections P1 and P2, then unification produces a
166+
> variable X and asks us to prove that `ProjectionEq(P1 = X)` and
167+
> `ProjectionEq(P2 = X)`. (That used to be needed in an older system to
168+
> prevent cycles; I rather doubt it still is. -nmatsakis)

0 commit comments

Comments
 (0)