• calcopiritus@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    17 hours ago

    As a rust developer I feel obligated by religion to make this comment:

    Then you’d love rust! Rust only has “interfaces” (called traits) but doesn’t have inheritance. You just have traits that don’t inherit from anything and structs (that don’t inherit from other structs) that implement X amount of traits.

    So you can have the good things about OOP without the bad ones.

    And these traits allow you to make trait objects, which would be like regular objects in C# (with vtables for the methods). If 2 different structs implement the same trait, you can “downcast” them to a trait object and store them in the same array. Or pass it is an argument to a function that wants something that implements that trait but doesn’t care about the specific struct. You can of course cast it back later to the original struct.

    • SparroHawc@lemmy.zip
      link
      fedilink
      arrow-up
      2
      ·
      5 hours ago

      I like interfaces as a supplement to inheritance. The strength of inheritance is getting all of the internal functionality of the parent class, while still allowing you to differentiate between children.

      Interfaces are useful for disparate classes which don’t have much in common besides fitting within a specific use case, rather than classes that are very similar to each other but need specific distinguishing features.