Re:Abstraction without overhead: traits in Rust
by GolDDranks on Tuesday May 12, 2015 @07:58AM
(#9531032)
How would you put or index objects in a vector in C++ if they are virtual / dynamically sized? I'm in impression that unless you have the dynamically sized object behind a pointer, you get object slicing.
[ Parent
| on HN ]
Re:Abstraction without overhead: traits in Rust
by fgsfdsfgsfds on Tuesday May 12, 2015 @09:55AM
(#9531754)
I didn't say they were dynamically sized. You could either have objects of the same type, but with virtual functions, or you could make a union of all applicable objects (thus guaranteed to be constant sized, at the size of the largest object) and store those in the array/vector, switching on a type enum or calling a function from an inherited base class.
Re:Abstraction without overhead: traits in Rust
by haberman on Tuesday May 12, 2015 @05:48PM
(#9535175)
> You could either have objects of the same type, but with virtual functions
If objects have the same type, which is statically known, there is no need for virtual functions because the compiler can resolve the specific method implementation at compile-time.
> or you could make a union of all applicable objects (thus guaranteed to be constant sized, at the size of the largest object) and store those in the array/vector, switching on a type enum
You can do this in both C++ and Rust easily, with equivalent efficiency in both. In Rust you would just use an enum type. This design is generally highly discouraged though, because it requires callers to be aware of all possible "derived classes".
My comments were only about the case where you are using true language-level polymorphism.