• nous
    link
    fedilink
    English
    arrow-up
    5
    arrow-down
    1
    ·
    1 year ago

    Inheritance here is limiting. Composition creates a far nicer API. Take both go and rust, they use a set of different interfaces/traits that cover, Reader, Writer, Closer, Seeker. Not everything implements all of these and can implement any set of them. For instance, a File implements them all, as it can be read from, be written to, closed and arbitrarily seeked. But a TCP Stream cannot random seek to other locations inside it - you cannot suddenly reset a network connection to the start again, those bytes are lost. So even if they are both file deciphers they do have different behaviours.

    And these can be used for more than just files and network connections - you can write into or read from a vector/list of bytes, even seek randomly into it, but cannot close it as that does not make any sense.

    Most languages cannot have multiple inheritance - so you are stuck with a abstract class with all the functions on it with blank implementations so the unsupported functions become no-ops. But IMO that is just a hack to workaround the lack of being able to split these different types up.

    • dragontamer@lemmy.world
      link
      fedilink
      English
      arrow-up
      7
      arrow-down
      3
      ·
      1 year ago

      You’re not describing composition.

      Go Files do not “hasa reader”. You don’t do file.reader.read(), you just do file.read(), that’s inheritance as file has inherited the read() method.

      • colonial@lemmy.world
        link
        fedilink
        English
        arrow-up
        9
        arrow-down
        1
        ·
        1 year ago

        You’re confusing polymorphism for inheritance. read is a method on an interface that File implements - it is not inherited from a base class. You can use that File directly, or wherever a Reader interface (or whatever the name is, idk I don’t really do Go) is expected.

      • orangeboats@lemmy.world
        link
        fedilink
        English
        arrow-up
        5
        arrow-down
        1
        ·
        1 year ago

        Composition do not necessitate the creation of a new field like x.reader or x.writer, what are you on?

          • platypus_plumba@lemmy.world
            link
            fedilink
            English
            arrow-up
            2
            arrow-down
            1
            ·
            edit-2
            1 year ago

            Man, I honestly have no idea why they are downvoting you. Composition literally means taking common behavior and placing it in external objects behind interfaces using a has-a relationship.

            No idea why they are denying this. Inheritance vs composition is the same as “is-a” vs “has-a”. In composition re usability isn’t done via inheritance but code encapsulation.

            Saying that in Go objects can implement many interfaces is the Interface Segregation principle from SOLID. Basically having small interfaces instead of a big interface with methods that may not be implemented by all implementors.