3 comments

  • iTokio2 hours ago
    I almost never use inheritance beside using some kind of interfaces&#x2F;traits to declare a contract.<p>However, the only time where I’m missing code&#x2F;data reuse through inheritance is with GUI. Some mostly flat hierarchies of widgets are really powerful ways to declare and compose UI components with shared behaviors.<p>In rust, the DX for GUI components is always lacking compared to web, C#. With maybe the exception of Slint which is really not Rust anymore.<p>Is there a way to have good DX for GUI components in Rust?
    • aabhay1 hour ago
      If you want to get inspired by good component DX, try looking at Bevy, the game engine.<p>But essentially it comes down to traits, newtypes&#x2F;enum variants, and macros.
  • pdpi4 hours ago
    Cool catalogue, but it had nothing to do with inheritance. It’s more “nine ways to do polymorphism-adjacent things using ad-hoc polymorphism (“traits”) instead of subtype polymorphism (‘inheritance’)”
    • nextaccountic3 hours ago
      Just like you can do OO in any language - for example, you can do OO like C (or at least that&#x27;s what GTK folks believe), Rust already has most runtime mechanisms to pretty much implement inheritance in a library or a coding pattern (as opposed to a language feature)<p>For a long time the most important thing missing was a better story around upcasting, but Rust 1.86 implemented the usual child-to-parent upcasting [0] expected in OO (with the Any trait doing the parent-to-child downcasting [1] in a way that&#x27;s less error prone than most OO languages actually)<p>Also note that Firefox and Servo were a early proponents of getting more OO into Rust (I guess that&#x27;s because they need to deal with the DOM? Not sure). Which doesn&#x27;t mean literally having classes and inheritance, but rather, language features that can emulate those when needed.<p>Today I think the missing piece is fields in traits: a way for a vtable to store field offsets rather than just pointers to method; current practice is to add getters and setters to traits when needed (but that&#x27;s &quot;ugly&quot;). Unfortunately discussion around this stalled [2]<p>[0]: <a href="https:&#x2F;&#x2F;blog.rust-lang.org&#x2F;2025&#x2F;04&#x2F;03&#x2F;Rust-1.86.0&#x2F;#trait-upcasting" rel="nofollow">https:&#x2F;&#x2F;blog.rust-lang.org&#x2F;2025&#x2F;04&#x2F;03&#x2F;Rust-1.86.0&#x2F;#trait-upc...</a><p>[1]: <a href="https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;any&#x2F;index.html" rel="nofollow">https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;any&#x2F;index.html</a><p>[2]: maybe the latest one is <a href="https:&#x2F;&#x2F;internals.rust-lang.org&#x2F;t&#x2F;fields-in-traits&#x2F;6933&#x2F;" rel="nofollow">https:&#x2F;&#x2F;internals.rust-lang.org&#x2F;t&#x2F;fields-in-traits&#x2F;6933&#x2F;</a> but idk
    • Animats2 hours ago
      Yes. The polymorphism isn&#x27;t bad. It&#x27;s the inheritance of data that&#x27;s the problem.<p>Rust traits can&#x27;t have associated data. That&#x27;s the big difference from objects.<p>It&#x27;s also hard to have a parent-child relationship where the child can find the parent. That takes much fooling around with wordy combos of Rc, Weak, upgrade, etc.<p>This makes it hard to convert C++ programs, and C++ programmers, to Rust. It&#x27;s quite possible to write 100% safe code under Rust&#x27;s constraints, but it takes a major rethink from C++.
  • zozbot2343 hours ago
    Much of this post seems to focus on interface inheritance via traits, which is a well-known feature of Rust. It&#x27;s weird though that it completely misses the generic typestate pattern, which is pretty unique to Rust as far as it goes and can be quite fairly described as OOP <i>implementation</i> inheritance in a type-theoretic trench coat.