Why avoid subtyping?

前端 未结 8 934
长发绾君心
长发绾君心 2020-12-02 07:55

I have seen many people in the Scala community advise on avoiding subtyping \"like a plague\". What are the various reasons against the use of subtyping? What are the altern

8条回答
  •  独厮守ぢ
    2020-12-02 08:05

    I think the general context is for the lanaguage to be as "pure" as possible (ie using as much as possible pure functions), and comes from the comparison with Haskell.
    From "Ruminations of a Programmer"

    Scala, being a hybrid OO-FP language has to take care of issues like subtyping (which Haskell does not have).

    As mentioned in this PSE answer:

    no way to restrict a subtype so that it can't do more than the type it inherits from.
    For example, if the base class is immutable and defines a pure method foo(...), derived classes must not be mutable or override foo() with a function that is not pure

    But the actual recommendation would be to use the best solution adapted to the program you are currently developing.

提交回复
热议问题