@@ -2118,10 +2118,10 @@ impl Circle: Shape {
21182118let 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
21262126For 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 {
21512151This 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
21562184The above allows us to define functions that polymorphically act on
0 commit comments