Hello,
I can't find a solution to a polymorphic problem. I construct a system to compute a value but I want it to be generic so it doesn't have to know on what type it computes. So I use generic and it works fine. Now the system store the object on what it computes in a collection ([]). The object are stored them computed.
I've done a sample code :
pub trait Base {
    fn do_base(&self);
}

struct TestBase;

impl Base for TestBase    {
    fn do_base(&self)    {
        println!("ici");
    }
}

trait GenerciFn    {
    fn do_generic<T: Base>(&self, base: &T);
}

struct DoGenericFn;

impl GenerciFn for DoGenericFn {
    fn do_generic<T: Base>(&self, base: &T)    {
        base.do_base();
    }
}

struct ToTestStr    {
    vec_gen: ~[~TestBase],
}

impl ToTestStr    {
    fn testgencall<T: GenerciFn>(&self, gen: &T)    {
        for base in self.vec_gen.iter()    {
            //let test = base as &~TestBase;
            gen.do_generic(&**base);
        }
    }
}

#[main]
fn main() {
    let base = TestBase;
    let test = ToTestStr {vec_gen: ~[~base],};
    let gen = DoGenericFn;
    test.testgencall(&gen);

}

This code work but I would like to replace the vec_gen: ~[~TestBase], with vec_gen: ~[~Base]. I didn't find a solution. I'am not shure it's possible because it's sort of dynamic polymorphism that it's very hard to do at compile time. How the compiler know that my collection contains TestBase. I think I don't use the right way to do this in Rust. Perhaps I'am too used to dynamic polymorphic language like Java, C#, .... What I want is that my lib doesn't know the real Base type use but the user's lib knows and tell by adding the right type to a collection.

Philippe Delrieu

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to