On 28/11/13 20:26, Rémi Fontan wrote:
Hi,

would you know what is the cost of implementing the double dispatch technique as described on following link? (section "What if I want overloading?")

http://smallcultfollowing.com/babysteps/blog/2012/10/04/refining-traits-slash-impls/

does using traits means making used of something similar to vtable? Or would the compiler optimise those double dispatch to a very optimise code?

The motivation is to have multiple implementation of mathematical operators for struct like vector 2d, 3d, matrices, and having them as efficient as possible is important.

cheers,

Rémi


Generics are always monomorphised to be static dispatch; explicit trait objects (~Trait, &Trait, etc) are the only times that one gets dynamic dispatch (using a vtable). So, trait methods be purely compile time if you never box the types into trait objects, i.e.

    fn static_with_generics<T: Trait>(x: &T) { ... }
    fn dynamic_with_vtables(x: &Trait) { ... }

The layered traits like in the example in that blog-post are the same as normal generics, just the compiler has to do a little more work (but this is only at compile time).

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

Reply via email to