The Rust way to do that would be to have an array of objects that all
implement the needed trait. But that would mean that access to the data
members would be a "virtual function call", while access to the data
members of the new kind of traits you described would be cheap.
I think that introducing two kinds of traits would have non-obvious
implications on the type system, in addition to saddling us with all the
pains of a single-inheritance types hierarchy system.
Also, assuming you do introduce two types of traits, for the reason you
described, making implementation reuse a feature of _only_ one kind of
traits feels very wrong. There should be some way to do reasonable
implementation reuse for the current kind of traits as well.
You could view the new kind of traits you described as a special case of
what I described. In fact you could say that `pub struct Foo : Bar { ... }`
means exactly `pub struct Foo { reuse Bar::*; ... }`, and also allow the
kind of pointer casts/heterogeneous arrays you are talking about.
Or, we could move in the direction of anonymous members, which should also
allow for the kind of pointer casts/heterogeneous arrays you want... These
would offer a somewhat different tradeoff than the source code
transformation approach, so you might like them better :-)
On Tue, Nov 12, 2013 at 9:56 AM, Patrick Walton <[email protected]>wrote:
> On 11/12/13 4:53 PM, Oren Ben-Kiki wrote:
>
>> "Your solution does not match the performance of single inheritance,
>> because it has virtual method calls for every field access from the
>> "outside" of the trait."
>>
>> Sorry, I don't follow.
>>
>> Access to members from outside of the traits would require accessing the
>> concrete type. Since the approach I described used source
>> transformation, then this would just be a normal data member access, as
>> efficient as single inheritance.
>>
>
> Oh, source transformation. I didn't read closely. That won't work as a
> replacement for single inheritance because it doesn't provide truly
> existential types--for example, an array of heterogeneous objects all of
> which share a common prefix.
>
> Patrick
>
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev