3 ms·
Technically traits are polymorphism (and you could argue Rust's enums are too), they just look differently than in classically object-oriented languages What R
by brundolf 4y ago
Technically traits are polymorphism (and you could argue Rust's enums are too), they just look differently than in classically object-oriented languages
What Rust really skips is inheritance (although you can get partway there with trait dependencies), which I think is generally a good thing
- tialaramex 4y agoSpecifically Rust doesn't have implementation inheritance. The fact that [u8; N] and Vec<String> are both IntoIter doesn't imply they are somehow inheriting any common elements of their implementation, just that they both provide means to convert themselves into Iterators when needed. In Rust we could make a Mimic by composing a Chest, and a Monster, and then implementing any traits on the resulting composite object appropriately, passing through to an underlying object in some cases -- opening this "Chest" ought to cause its status as a Monster to be revealed right?. In languages with single inheritance we need to decide, is a Mimic a Chest? Is it a Monster? Maybe all Chests are Monsters? Maybe all Monsters are Chests? In languages with multiple inheritance we can inherit from both, but we might find that isn't what we wanted either and there are likely to be conflicts from such a web of heritability.