5 ms·
I'm finding that my OO code style these days is very composition heavy with pretty much no inheritance use. I think given Rust's type system, I'm not going to m
by Ycros 13y ago
I'm finding that my OO code style these days is very composition heavy with pretty much no inheritance use. I think given Rust's type system, I'm not going to miss inheritance much.
- Daishiman 13y agoIt's a question of performance more than composition.
- ehsanu1 13y agoPlease elaborate?
- eridius 13y agoI believe Rust wants inheritance primarily for Servo, because the DOM is defined in terms of inheritance.
- pcwalton 13y agoRight. Idiomatic Rust prefers composition over inheritance.
- zanny 13y agoEvery language in the universe prefers composition over inheritance =P
- seanmcdirmid 13y agoNot very true. Some languages prefer inheritance to express is-a relationships and composition to express has-a relationships. Some languages try to shoe horn is-a into being equivalent to has-a, but they usually have trouble expressing non-structural subtyping.
- banachtarski 13y agoThe ones that don't blossom into interface factories which make factory adapters of abstract factories. (I'm looking at you, Java)
- V-2 13y agoFor the lack of a nail, throw new HorseshoeNailNotFoundException("no nails!"); For the lack of a horseshoe, EquestrianDoctor.getLocalInstance().getHorseDispatcher().shoot(); (...) http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom...
- jeltz 13y agoIf that is the only use case I would rather want another solution to keep the language minimal. Just like the garbage collection was moved to a library.
- dbaupp 13y agoI think that the core team thought about this long and hard, but it was decided that there was no other solution. A virtual method call to look up some property shared by all the DOM nodes was just too expensive: a constant offset field look up is far far faster.
- pcwalton 13y agoCan't really do it. I tried. The type checker just plain has to know about inheritance for it to work and be safe. The good news is that's an extremely constrained sort of inheritance, very unlike traditional OO in that it doesn't have base classes--it's just a special extension to the trait (typeclass) system that allows you to require that structs begin with a certain set of fields. In fact, I don't even call it inheritance these days--I prefer the name "structural constraints".
- thirsteh 13y agoI think this is a good idea. It seems similar to Go's embedding: http://golang.org/doc/effective_go.html#embedding http://golang.org/doc/effective_go.html#embedding
- joelthelion 13y agoJust because people have overused inheritance in the past doesn't mean it's completely useless.
- masklinn 13y agoNobody has said it was useless. Although with the right language support, it is useless, composition can stand for it (demo: Self)
- joelthelion 13y agoBy that reasoning most programming features are useless (demo: C)
- masklinn 13y agoI'm reasonably certain you completely misunderstood my comment, because C definitely isn't an illustration of it.