On 3/11/14 12:09 PM, Bill Myers wrote:
I see a proposal to add "virtual struct" and "virtual fn" in the
workweek meeting notes, which appears to add an exact copy of Java's
OO system to Rust.

I think however that this should be carefully considered, and
preferably not added at all (or failing that, feature gated and
discouraged).

The core problem of "virtual functions" (shared by Java's classes,
etc.) is that rather than exposing a single public API, they expose
two: the API formed by public functions, and the API formed by
virtual functions to be overridden by subclasses, and the second API
is exposed in an inflexible and unclean way.

A much better way of allowing to override part of a struct's behavior
is by defining a trait with the overridable functionality, and
allowing to pass in an implementation of the trait to the base class,
while also providing a default implementation if desired.

This approach is not efficient enough for use cases like the DOM (two words on each pointer instead of one, for example), and doesn't allow for nonvirtual access of fields through a trait object.

Patrick

_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to