Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Trait inheritance/composition #2616

catamorphism opened this Issue Jun 15, 2012 · 4 comments


None yet
2 participants

catamorphism commented Jun 15, 2012

I know that there are multiple proposals floating around for how to do interface inheritance (of which traits may be one), but just noting it here so that FIXMEs can refer to issues.


catamorphism commented Nov 3, 2012

Though this is an old bug, it's still useful since inheritance isn't quite finished yet. I moved it from enhancement => completion.

struct Point {
  mut x: int,
  mut y: int

trait Positioned {
  fn X()-> int;
  fn Y()-> int;
  fn SetX(int);
  fn SetY(int);

trait Movable: Positioned {
  fn translate(dx: int, dy: int) {
    self.SetX(self.X() + dx);
    self.SetY(self.Y() + dy);

struct Entity {
  mut pos: Point,

impl Entity: Positioned {
  fn X()-> int { self.pos.x }
  fn Y()-> int { self.pos.y }
  fn SetX(v: int) { self.pos.x = v; }
  fn SetY(v: int) { self.pos.y = v; }

impl Entity: Movable { }

fn main() {
  let mut e: Entity = Entity {
    pos: Point {x: 0, y: 0},

  io::println(fmt!("%?, %?", e.X(), e.Y()));
  e.translate(5, 10);
  io::println(fmt!("%?, %?", e.X(), e.Y()));

"error: attempted access of field SetX on type self, but no field or method with that name was found"

If you change the Movable.translate method to:

trait Movable: Positioned {
  fn translate(dx: int, dx: int) {
    let s = self as Positioned;
    s.SetX(s.X() + dx);
    s.SetY(s.Y() + dy);

It says: "error: failed to find an implementation of trait @positioned for self"


catamorphism commented Nov 16, 2012

@dustinlacewell I filed your bug as a separate issue: #3979


catamorphism commented Dec 7, 2012

Inheritance basically works, closing the catchall bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment