Associated types
The use of "Associated types" improves the overall readability of code by moving inner types locally into a trait as output types. Syntax for the trait definition is as follows:
// `A` and `B` are defined in the trait via the `type` keyword.
// (Note: `type` in this context is different from `type` when used for
// aliases).
trait Contains<T> {
type A;
type B;
// Updated syntax to refer to these new types generically.
fn contains(self: @T, a: @Self::A, b: @Self::B) -> bool;
}
Note that functions that use the trait Contains are no longer required to express A or B at all:
// Without using associated types
fn difference<A, B, C, +Contains<C, A, B>>(container: @C) -> i32 { ... }
// Using associated types
fn difference<T, +Contains<T>>(container: @T) -> i32 { ... }
Let's rewrite the example using associated types:
#[derive(Drop)]
struct Container {
first: i32,
last: i32,
}
// A trait which checks if 2 items are stored inside of container.
// Also retrieves first or last value.
trait Contains<T> {
// Define generic types here which methods will be able to utilize.
type A;
type B;
fn contains(self: @T, a: @Self::A, b: @Self::B) -> bool;
fn first(self: @T) -> i32;
fn last(self: @T) -> i32;
}
impl ContainerImpl of Contains<Container> {
// Specify what types `A` and `B` are. If the `input` type
// is `Container{first: i32, last: i32}`, the `output` types are determined
// as `i32` and `i32`.
type A = i32;
type B = i32;
// `@Self::A` and `@Self::B` are also valid here.
fn contains(self: @Container, a: @i32, b: @i32) -> bool {
self.first == a && self.last == b
}
// Grab the first number.
fn first(self: @Container) -> i32 {
*self.first
}
// Grab the last number.
fn last(self: @Container) -> i32 {
*self.last
}
}
fn difference<T, +Contains<T>>(container: @T) -> i32 {
container.last() - container.first()
}
fn main() {
let number_1: i32 = 3;
let number_2: i32 = 10;
let container = Container { first: number_1, last: number_2 };
println!(
"Does container contain {} and {}: {}",
number_1,
number_2,
container.contains(@number_1, @number_2),
);
println!("First number: {}", container.first());
println!("Last number: {}", container.last());
println!("The difference is: {}", difference(@container));
}