[rust-dev] Representing foreign class inheritance in Rust

Mikhail Zabaluev mikhail.zabaluev at gmail.com
Thu Oct 2 15:40:24 PDT 2014


Hi,

While working on bindings to the GObject type system, I could not help
noticing the amount of boilerplate I ended up with trying to represent
OOP-style class inheritance:

https://gist.github.com/mzabaluev/02202b4a8819687094be#file-foreign-inheritance-and-cast-rs-L18

The trait reform and UFCS should take away much of the pain, but a really
nice solution would be a way to annotate `impl A for InstA` as
auto-derivable or "inheritable", so that any type implementing
`Inherit<InstA>` (here, a proposed special standard trait) would
automatically get its own implementation of `A` in terms of the upcast
provided by `Inherit`. Furthermore, that implicit trait implementation
would in turn be "inheritable", so that any indirect subclass would
implement `A`, and any other inheritable traits in the chain of its
parents, for free.

Not all traits would be available for such treatment; dependencies on
static properties of Self a la `fn new() -> Self` would preclude it. But
the traits that a classic OOP hierarchy lends itself to, should be just
what the doctor ordered.

Note also how the default implementations of the inheritable trait methods
are amenable to tail call elimination. If the compiler itself would
generate those shims, it could just emit inherited impls' vtable entries as
trampolines to the base impl, and similarly apply the tail-call hinting
during static monomorphization.

Does the above make sense, or am I getting the whole subject wrong and
there is (or will soon be) a better Rust idiomatic for that? Care for an
RFC pull request?

Mikhail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20141003/cd2a946d/attachment.html>


More information about the Rust-dev mailing list