I was about to start another discussion about this, but from a different angle;
I'd start with the pragmatic observation: there is a lot of existing working software that can take internal-vtable based plugins,
.. and ask if there's a way that the trait-viable mechanism could be generalised to the level that the vtables could be anywhere ((i) fat pointer, (ii) embedded as the first item of a struct, (iii) or calculated by some generic means from the address and other information in the struct - imagine for example being able to retroactively get from an enum tag to a vtable. apparently some java implementations don't actually store a vtable in the object like C++ but rather store them at the start of contiguous arrays of homogeneous objects.. thats actually quite interesting. maybe a way to leverage the MMU, mapping ranges such that calculating the vtable ptr is very easy.
Let me refresh my memory, I think there was some highly unsafe hack that almost allowed internal vtables... I remember some messing around with cast::transmute and the deref traits , which got to one level; the question would be 'could extra language support allow this to be done in a less unsafe way' - exposing a way of binding a vtable with the underlying data, or querying the vtable of a trait on it's own.
(I can't remember off hand if rust even has plain function pointers these days, i realise you could roll them with that)
r.e. "classes", I haven't tried but i'd imagine you can make macros that declare things more concisely.
tangent;-
whilst rust macros are really useful, macros generally spike a negative reaction in my head on 2 fronts:-
- it might be C prejudice, but the idea that they are a 'hack'
- further justification: language features can be reasoned about at compile time and get much better error messages; they can be written using the rest of the language's syntax, as such I think 'a more powerful language' is a more efficient use of the whole world's resources. when using macros, it's usually for something another language can do better with a 'more solid' feature.
-plain syntax issues which might be fixeable through other requests:
the extra nesting level and disruption of existing declaration pattern really bug me (i wish it didn't). It's because Rusts' "pattern" of declarations is
defining_keyword name {
content
}
you can't replicate this with a macro; as such macro-base declarations stick out from the rest of the language like a sore thumb.
( I know lisps make great use of macros but there everything looks the same..)
if you could write
class! MyCppStyleObject {
...
}
that might help a lot perceptually (a preceding ident in macro-rules?).. but I've no idea how feasible it would be r.e. parsing though (maybe it would be asking too much to be able to allow anything between the ident and the 'main body', as we'd need if we wanted to write class! Foo : Bar { ..}
back to the topic of the thread,
r.e. the mention above that these vtables are an 'anti-pattern' (premature binding?) I think it's a case of "not throwing out the baby with the bathwater" .. the fact is they can still be useful.. there's a sliding scale of runtime efficiency, syntactic convenience, versatility . I would personally like to see a sliding scale of versatility where you don't have to bake any one path early on into the syntax. 'openly derivable classes' can still be used for code that queries the type (e.g. if (auto *p=dynamic_cast(x)){ .. } etc etc) . you could imagine 'rolling an enum' being like 'declaring a base class and a fixed set of derived classes', and you could use it both ways (e.g. still derived new classes, still make vtable entires for the enum..). (imagine, coming at it the other way, if you could generalise computing the tag of an enum.. such that the tag could just be an internal vtable pointer .. an enum could even implement itself as a class by doing a whole-program 'gather' of all the useages?)
Such blurring of the options might have utility in refactoring and in experimentation, e.g. a sourcebase might go through various options until it discovers the right layout