Skip to content

Commit 850050b

Browse files
committed
doc: 'trait constraints' -> 'inheritance'. Expand
/cc: #4217
1 parent 66aadec commit 850050b

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

doc/rust.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,17 +1222,44 @@ impl float: Num {
12221222
let x: float = Num::from_int(42);
12231223
~~~~
12241224

1225-
Traits can have _constraints_ for example, in
1225+
Traits may inherit from other traits. For example, in
12261226

12271227
~~~~
12281228
trait Shape { fn area() -> float; }
12291229
trait Circle : Shape { fn radius() -> float; }
12301230
~~~~
12311231

12321232
the syntax `Circle : Shape` means that types that implement `Circle` must also have an implementation for `Shape`.
1233+
Multiple supertraits are separated by spaces, `trait Circle : Shape Eq { }`.
12331234
In an implementation of `Circle` for a given type `T`, methods can refer to `Shape` methods,
12341235
since the typechecker checks that any type with an implementation of `Circle` also has an implementation of `Shape`.
12351236

1237+
In type-parameterized functions,
1238+
methods of the supertrait may be called on values of subtrait-bound type parameters.
1239+
Refering to the previous example of `trait Circle : Shape`:
1240+
1241+
~~~
1242+
# trait Shape { fn area() -> float; }
1243+
# trait Circle : Shape { fn radius() -> float; }
1244+
fn radius_times_area<T: Circle>(c: T) -> float {
1245+
// `c` is both a Circle and a Shape
1246+
c.radius() * c.area()
1247+
}
1248+
~~~
1249+
1250+
Likewise, supertrait methods may also be called on trait objects.
1251+
1252+
~~~ {.xfail-test}
1253+
# trait Shape { fn area() -> float; }
1254+
# trait Circle : Shape { fn radius() -> float; }
1255+
# impl int: Shape { fn area() -> float { 0.0 } }
1256+
# impl int: Circle { fn radius() -> float { 0.0 } }
1257+
# let mycircle = 0;
1258+
1259+
let mycircle: Circle = @mycircle as @Circle;
1260+
let nonsense = mycircle.radius() * mycircle.area();
1261+
~~~
1262+
12361263
### Implementations
12371264

12381265
An _implementation_ is an item that implements a [trait](#traits) for a specific type.

doc/tutorial.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,10 +2118,10 @@ impl Circle: Shape {
21182118
let s: Circle = Shape::new_shape(42.5);
21192119
~~~~
21202120

2121-
## Trait constraints
2121+
## Trait inheritance
21222122

2123-
We can write a trait declaration that is _constrained_ to only be implementable on types that
2124-
also implement some other trait.
2123+
We can write a trait declaration that _inherits_ from other traits, called _supertraits_.
2124+
Types that implement a trait must also implement its supertraits.
21252125

21262126
For example, we can define a `Circle` trait that only types that also have the `Shape` trait can have:
21272127

@@ -2151,6 +2151,34 @@ impl CircleStruct: Shape {
21512151
This is a silly way to compute the radius of a circle
21522152
(since we could just return the `circle` field), but you get the idea.
21532153

2154+
In type-parameterized functions,
2155+
methods of the supertrait may be called on values of subtrait-bound type parameters.
2156+
Refering to the previous example of `trait Circle : Shape`:
2157+
2158+
~~~
2159+
# trait Shape { fn area() -> float; }
2160+
# trait Circle : Shape { fn radius() -> float; }
2161+
fn radius_times_area<T: Circle>(c: T) -> float {
2162+
// `c` is both a Circle and a Shape
2163+
c.radius() * c.area()
2164+
}
2165+
~~~
2166+
2167+
Likewise, supertrait methods may also be called on trait objects.
2168+
2169+
~~~ {.xfail-test}
2170+
# trait Shape { fn area() -> float; }
2171+
# trait Circle : Shape { fn radius() -> float; }
2172+
# impl int: Shape { fn area() -> float { 0.0 } }
2173+
# impl int: Circle { fn radius() -> float { 0.0 } }
2174+
# let mycircle = 0;
2175+
2176+
let mycircle: Circle = @mycircle as @Circle;
2177+
let nonsense = mycircle.radius() * mycircle.area();
2178+
~~~
2179+
2180+
> ***Note:*** Trait inheritance does not actually work with objects yet
2181+
21542182
## Trait objects and dynamic method dispatch
21552183

21562184
The above allows us to define functions that polymorphically act on

0 commit comments

Comments
 (0)