I've actually come around and now I'm starting to actually like this
virtual struct proposal, although not necessarily the example syntax (yay
bikeshed!). What brought me around is that if you squint your eyes a bit,
the example was mashing together struct inheritance and virtual methods
into one syntax. If we decouple the two we can get a more rust-y syntax (to
me) if we reify the anonymous trait into something that can be implemented
by substructures:

```
// Unsized structs are unsized but *can't* end with an unsized field (so
they can be extended)
unsized struct Base { id: int }
unsized struct Foo: Base { }
struct Bar: Foo  { }

impl Base {
    fn a(); // impls can now have unimplemented methods
    fn b() { }
}

impl Base for Foo {
    fn a() { }
}

impl Foo {
    fn c() { }
}

impl Base for Bar {}
impl Foo for Bar {}

fn do_something<T: Base>(t: &Base) {
    println!("{}", t.id); // compiler can figure out the exact field offset
because `Base` is a struct trait
}

fn main() {
    let bases = ~[
        ~Foo { id: 1 } as ~Base, // compiler uses the more optimal
`~(*vtable, obj)` because `Base` is a struct trait
        ~Bar { id: 2 } as ~Base,
    ];
    for base in bases.iter() {
        do_something(base);
    }
}
```

One interesting thing we could do with this is allow named traits to also
derive from struct traits. This could allow parallel virtual method trees:

```
unsized struct Base { id: int }
unsized struct Foo: Base { }
struct Bar: Foo  { }

trait BaseTrait1: Base {
    fn foo(&self) {
        println!("{}", self.id); // exact offset because id comes from a
struct trait
    }
}
impl BaseTrait1 for Base { }
impl BaseTrait1 for Foo { }
impl BaseTrait1 for Bar { }

trait BaseTrait2: Base {
    fn bar(&self) {
        println!("{}", self.id); // exact offset because id comes from a
struct trait
    }
}
impl BaseTrait2 for Base { }
impl BaseTrait2 for Foo { }
impl BaseTrait2 for Bar { }

fn main() {
    let bases = ~[ ~Foo as ~BaseTrait1, ~Bar as ~BaseTrait1 ]; // compiler
uses more optimal ~(*vtable, obj)` layout
    for base in bases.iter() { base.foo() }

    let bases = ~[ ~Foo as ~BaseTrait2, ~Bar as ~BaseTrait2 ];
    for base in bases.iter() { base.foo() }
}
```

I'm not sure if there are any practical applications for this though.
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to