Skip to content

Commit

Permalink
Talk about trait bounds in the tutorial.
Browse files Browse the repository at this point in the history
  • Loading branch information
bblum committed Aug 23, 2013
1 parent 2c0f9bd commit c678b22
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion doc/tutorial.md
Expand Up @@ -1864,7 +1864,7 @@ so you could not apply `head` to a type
that does not implement `Clone`.

While most traits can be defined and implemented by user code,
two traits are automatically derived and implemented
three traits are automatically derived and implemented
for all applicable types by the compiler,
and may not be overridden:

Expand All @@ -1877,6 +1877,12 @@ These are types that do not contain anything intrinsically mutable.
Intrinsically mutable values include `@mut`
and `Cell` in the standard library.

* `'static` - Non-borrowed types.
These are types that do not contain any data whose lifetime is bound to
a particular stack frame. These are types that do not contain any
borrowed pointers, or types where the only contained borrowed pointers
have the `'static` lifetime.

> ***Note:*** These two traits were referred to as 'kinds' in earlier
> iterations of the language, and often still are.
Expand Down Expand Up @@ -2135,6 +2141,30 @@ select the method to call at runtime.

This usage of traits is similar to Java interfaces.

By default, each of the three storage classes for traits enforce a
particular set of built-in kinds that their contents must fulfill in
order to be packaged up in a trait object of that storage class.

* The contents of owned traits (`~Trait`) must fulfill the `Send` bound.
* The contents of managed traits (`@Trait`) must fulfill the `'static` bound.
* The contents of borrowed traits (`&Trait`) are not constrained by any bound.

Consequently, the trait objects themselves automatically fulfill their
respective kind bounds. However, this default behavior can be overridden by
specifying a list of bounds on the trait type, for example, by writing `~Trait:`
(which indicates that the contents of the owned trait need not fulfill any
bounds), or by writing `~Trait:Send+Freeze`, which indicates that in addition
to fulfilling `Send`, contents must also fulfill `Freeze`, and as a consequence,
the trait itself fulfills `Freeze`.

* `~Trait:Send` is equivalent to `~Trait`.
* `@Trait:'static` is equivalent to `@Trait`.
* `&Trait:` is equivalent to `&Trait`.

Builtin kind bounds can also be specified on closure types in the same way (for
example, by writing `fn:Freeze()`), and the default behaviours are the same as
for traits of the same storage class.

## Trait inheritance

We can write a trait declaration that _inherits_ from other traits, called _supertraits_.
Expand Down

0 comments on commit c678b22

Please sign in to comment.