Why couldn't we implement multiple inheritance like this:
Traits provide the functionality, and a struct provides the data for those
traits to implement their functionality.
It would look like this:
trait Inflate {
fn get_radius<'s>(&'s mut self) -> &'s mut int;
fn inflate_by(&mut self, amount: int) {
*self.get_radius() += amount;
}
// ... rest of the implementation ...
}
trait Flatten {
fn get_radius<'s>(&'s mut self) -> &'s mut int;
fn release_all_air(&mut self) {
*self.get_radius() = 0;
}
// ... rest of the implementation ...
}
struct Balloon {
radius: int
}
impl Inflate for Balloon {
fn get_radius<'s>(&'s mut self) -> &'s mut int {
&mut self.radius
}
}
impl Flatten for Balloon {
fn get_radius<'s>(&'s mut self) -> &'s mut int {
&mut self.radius
}
}
fn main() {
let mut b = Balloon { radius: 123 };
b.inflate_by(10);
b.release_all_air();
}
And we could provide some nicer syntax for doing that, something like this:
trait Inflate {
ball_radius: mut int,
fn inflate_by(&mut self, amount: int) {
self.ball_radius += amount;
}
// ... rest of the implementation ...
}
trait Flatten {
balloon_size: mut int,
fn release_all_air(&mut self) {
self.balloon_size = 0;
}
// ... rest of the implementation ...
}
struct Balloon {
radius: int
}
impl Inflate for Balloon {
ball_radius => self.radius
}
impl Flatten for Balloon {
balloon_size => self.radius
}
-Tommi
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev