Comment

Comment by josephg

> I don't think Inheritance is always bad - sometimes it's a useful tool.

I can only think of one or two instances where I've really been convinced that inheritance is the right tool. The only one that springs to mind is a View hierarchy in UI libraries. But even then, I notice React (& friends) have all moved away from this approach. Modern web development usually makes components be functions. (And yes, javascript supports many kinds of inheritance. Early versions of react even used them for components. But it proved to be a worse approach.)

I've been writing a lot of rust lately. Rust doesn't support inheritance, but it wouldn't be needed in your example. In rust, you'd implement that by having a trait with functions (+default behaviour). Then have each robot type implement the trait. Eg:

    trait Robot {
        fn stop(&mut self) { /* default behaviour */ }
    }

    struct BenderRobot;
    
    impl Robot for BenderRobot {
        // If this is missing, we default to Robot::stop above.
        fn stop(&mut self) { /* custom behaviour */ }
    }
parent josephg 1 year ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton