Simple single inheritance one day?


#1

Single inheritance is just about the only thing I miss from C++. I am no Rust expert, but seems it needs two things: able to have one struct extend another, and able to have casting ability (automatic upcasts, and explicit downcasts).

Currently I have the horrible hack (I know it’s terrible):

#[repr(C, packed)]
struct Monster<T> {
    x : i32,
    y : i32,
    hitpoints : i32,
    extra_data : T
}

#[repr(C, packed)]
struct Demon {
    fireballs : i32,
}

Then we create Monster<Demon> and access its data. Casting up and down seems to work, but maybe I am just lucky and the language never meant this. What seems to be needed is something like:

struct Monster {
        x : i32,
        y : i32,
        hitpoints : i32
}

struct Demon : Monster {
    fireballs : i32
}

Would be helpful if a function accepting &Monster would also accept &Demon. And adding a forced downcast may be possible, if often evil. Again I am no Rust expert so I don’t know any of the more thorny issues, I will just use my hacked generics method until there is a better option.

Steve


#2

Hey Steve!

We’ve been talking about some kind of inheritance, maybe, for a long time. We’re still working through various proposals. http://aturon.github.io/blog/2015/09/18/reuse/ is @aturon’s latest one, and references @nikomatsakis’s, among others. You might want to check it out :smile: