Skip to content

Commit 7dc042d

Browse files
committed
---
yaml --- r: 34229 b: refs/heads/snap-stage3 c: 850050b h: refs/heads/master i: 34227: ef7644e v: v3
1 parent 02f8759 commit 7dc042d

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 66aadecfee10784bc5702b2fe88d4414058171b1
4+
refs/heads/snap-stage3: 850050b7dfad4dbc241d299842c7f8e12b2053e9
55
refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/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.

branches/snap-stage3/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)