2011-10-31 10:18:59 -05:00
|
|
|
# Generics
|
|
|
|
|
|
|
|
## Generic functions
|
|
|
|
|
2012-01-12 05:56:23 -06:00
|
|
|
Throughout this tutorial, I've been defining functions like `for_rev`
|
|
|
|
that act only on integers. It is 2012, and we no longer expect to be
|
|
|
|
defining such functions again and again for every type they apply to.
|
|
|
|
Thus, Rust allows functions and datatypes to have type parameters.
|
2011-10-31 10:18:59 -05:00
|
|
|
|
|
|
|
fn for_rev<T>(v: [T], act: block(T)) {
|
2012-01-12 05:56:23 -06:00
|
|
|
let i = vec::len(v);
|
2011-10-31 10:18:59 -05:00
|
|
|
while i > 0u {
|
|
|
|
i -= 1u;
|
|
|
|
act(v[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-12 05:56:23 -06:00
|
|
|
fn map<T, U>(v: [T], f: block(T) -> U) -> [U] {
|
2011-10-31 10:18:59 -05:00
|
|
|
let acc = [];
|
|
|
|
for elt in v { acc += [f(elt)]; }
|
|
|
|
ret acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
When defined in this way, these functions can be applied to any type
|
|
|
|
of vector, as long as the type of the block's argument and the type of
|
|
|
|
the vector's content agree with each other.
|
|
|
|
|
|
|
|
Inside a parameterized (generic) function, the names of the type
|
|
|
|
parameters (capitalized by convention) stand for opaque types. You
|
|
|
|
can't look inside them, but you can pass them around.
|
|
|
|
|
|
|
|
## Generic datatypes
|
|
|
|
|
2012-01-12 05:56:23 -06:00
|
|
|
Generic `type` and `enum` declarations follow the same pattern:
|
2011-10-31 10:18:59 -05:00
|
|
|
|
|
|
|
type circular_buf<T> = {start: uint,
|
|
|
|
end: uint,
|
|
|
|
buf: [mutable T]};
|
|
|
|
|
2012-01-12 05:56:23 -06:00
|
|
|
enum option<T> { some(T); none; }
|
2011-10-31 10:18:59 -05:00
|
|
|
|
|
|
|
You can then declare a function to take a `circular_buf<u8>` or return
|
|
|
|
an `option<str>`, or even an `option<T>` if the function itself is
|
|
|
|
generic.
|
|
|
|
|
2012-01-12 05:56:23 -06:00
|
|
|
The `option` type given above exists in the core library as
|
|
|
|
`option::t`, and is the way Rust programs express the thing that in C
|
|
|
|
would be a nullable pointer. The nice part is that you have to
|
2011-10-31 10:18:59 -05:00
|
|
|
explicitly unpack an `option` type, so accidental null pointer
|
|
|
|
dereferences become impossible.
|
|
|
|
|
|
|
|
## Type-inference and generics
|
|
|
|
|
|
|
|
Rust's type inferrer works very well with generics, but there are
|
|
|
|
programs that just can't be typed.
|
|
|
|
|
2012-01-12 05:56:23 -06:00
|
|
|
let n = option::none;
|
|
|
|
# n = option::some(1);
|
2011-10-31 10:18:59 -05:00
|
|
|
|
2011-11-21 04:56:31 -06:00
|
|
|
If you never do anything else with `n`, the compiler will not be able
|
2012-01-12 05:56:23 -06:00
|
|
|
to assign a type to it. (The same goes for `[]`, the empty vector.) If
|
|
|
|
you really want to have such a statement, you'll have to write it like
|
2011-10-31 10:18:59 -05:00
|
|
|
this:
|
|
|
|
|
2012-01-12 05:56:23 -06:00
|
|
|
let n2: option::t<int> = option::none;
|
2011-11-21 04:56:31 -06:00
|
|
|
// or
|
2012-01-12 05:56:23 -06:00
|
|
|
let n = option::none::<int>;
|
2011-10-31 10:18:59 -05:00
|
|
|
|
|
|
|
Note that, in a value expression, `<` already has a meaning as a
|
|
|
|
comparison operator, so you'll have to write `::<T>` to explicitly
|
|
|
|
give a type to a name that denotes a generic value. Fortunately, this
|
|
|
|
is rarely necessary.
|
|
|
|
|
|
|
|
## Polymorphic built-ins
|
|
|
|
|
|
|
|
There are two built-in operations that, perhaps surprisingly, act on
|
|
|
|
values of any type. It was already mentioned earlier that `log` can
|
2012-01-12 05:56:23 -06:00
|
|
|
take any type of value and output it.
|
2011-10-31 10:18:59 -05:00
|
|
|
|
2011-11-21 04:56:31 -06:00
|
|
|
More interesting is that Rust also defines an ordering for values of
|
|
|
|
all datatypes, and allows you to meaningfully apply comparison
|
|
|
|
operators (`<`, `>`, `<=`, `>=`, `==`, `!=`) to them. For structural
|
|
|
|
types, the comparison happens left to right, so `"abc" < "bac"` (but
|
|
|
|
note that `"bac" < "ác"`, because the ordering acts on UTF-8 sequences
|
|
|
|
without any sophistication).
|
|
|
|
|
|
|
|
## Kinds
|
|
|
|
|
2012-01-10 21:41:57 -06:00
|
|
|
<a name="kind"></a>
|
|
|
|
|
2011-11-21 04:56:31 -06:00
|
|
|
Perhaps surprisingly, the 'copy' (duplicate) operation is not defined
|
|
|
|
for all Rust types. Resource types (types with destructors) can not be
|
|
|
|
copied, and neither can any type whose copying would require copying a
|
|
|
|
resource (such as records or unique boxes containing a resource).
|
|
|
|
|
|
|
|
This complicates handling of generic functions. If you have a type
|
|
|
|
parameter `T`, can you copy values of that type? In Rust, you can't,
|
|
|
|
unless you explicitly declare that type parameter to have copyable
|
|
|
|
'kind'. A kind is a type of type.
|
|
|
|
|
2012-01-12 05:56:23 -06:00
|
|
|
## ignore
|
2011-11-21 04:56:31 -06:00
|
|
|
// This does not compile
|
|
|
|
fn head_bad<T>(v: [T]) -> T { v[0] }
|
|
|
|
// This does
|
2012-01-12 05:56:23 -06:00
|
|
|
fn head<T: copy>(v: [T]) -> T { v[0] }
|
2011-11-21 04:56:31 -06:00
|
|
|
|
|
|
|
When instantiating a generic function, you can only instantiate it
|
|
|
|
with types that fit its kinds. So you could not apply `head` to a
|
|
|
|
resource type.
|
|
|
|
|
|
|
|
Rust has three kinds: 'noncopyable', 'copyable', and 'sendable'. By
|
|
|
|
default, type parameters are considered to be noncopyable. You can
|
|
|
|
annotate them with the `copy` keyword to declare them copyable, and
|
|
|
|
with the `send` keyword to make them sendable.
|
|
|
|
|
|
|
|
Sendable types are a subset of copyable types. They are types that do
|
|
|
|
not contain shared (reference counted) types, which are thus uniquely
|
|
|
|
owned by the function that owns them, and can be sent over channels to
|
2012-01-12 05:56:23 -06:00
|
|
|
other tasks. Most of the generic functions in the core `comm` module
|
2011-11-21 04:56:31 -06:00
|
|
|
take sendable types.
|
2011-10-31 10:18:59 -05:00
|
|
|
|
|
|
|
## Generic functions and argument-passing
|
|
|
|
|
2012-01-12 05:56:23 -06:00
|
|
|
The previous section mentioned that arguments are passed by pointer or
|
|
|
|
by value based on their type. There is one situation in which this is
|
|
|
|
difficult. If you try this program:
|
2011-10-31 10:18:59 -05:00
|
|
|
|
2011-11-22 09:12:23 -06:00
|
|
|
# fn map(f: block(int) -> int, v: [int]) {}
|
2011-10-31 10:18:59 -05:00
|
|
|
fn plus1(x: int) -> int { x + 1 }
|
|
|
|
map(plus1, [1, 2, 3]);
|
|
|
|
|
|
|
|
You will get an error message about argument passing styles
|
|
|
|
disagreeing. The reason is that generic types are always passed by
|
|
|
|
pointer, so `map` expects a function that takes its argument by
|
|
|
|
pointer. The `plus1` you defined, however, uses the default, efficient
|
|
|
|
way to pass integers, which is by value. To get around this issue, you
|
|
|
|
have to explicitly mark the arguments to a function that you want to
|
2012-01-12 05:56:23 -06:00
|
|
|
pass to a generic higher-order function as being passed by pointer,
|
|
|
|
using the `&&` sigil:
|
2011-10-31 10:18:59 -05:00
|
|
|
|
2011-11-22 09:12:23 -06:00
|
|
|
# fn map<T, U>(f: block(T) -> U, v: [T]) {}
|
2011-10-31 10:18:59 -05:00
|
|
|
fn plus1(&&x: int) -> int { x + 1 }
|
|
|
|
map(plus1, [1, 2, 3]);
|
|
|
|
|
|
|
|
NOTE: This is inconvenient, and we are hoping to get rid of this
|
|
|
|
restriction in the future.
|