Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trait inheritance/composition #2616

Closed
catamorphism opened this issue Jun 15, 2012 · 4 comments
Closed

Trait inheritance/composition #2616

catamorphism opened this issue Jun 15, 2012 · 4 comments
Labels
A-trait-system Area: Trait system A-type-system Area: Type system
Milestone

Comments

@catamorphism
Copy link
Contributor

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
Copy link
Contributor Author

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

@dustinlacewell
Copy link

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
Copy link
Contributor Author

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

@catamorphism
Copy link
Contributor Author

Inheritance basically works, closing the catchall bug.

RalfJung pushed a commit to RalfJung/rust that referenced this issue Oct 26, 2022
rustup

With rust-lang#103360 having landed, I don't think we still need this `GetFileInformationByHandleEx` shim.
Aaron1011 pushed a commit to Aaron1011/rust that referenced this issue Jan 6, 2023
rustup

With rust-lang#103360 having landed, I don't think we still need this `GetFileInformationByHandleEx` shim.
celinval pushed a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

2 participants