Does Scala's pattern matching violate the Open/Closed Principle?

后端 未结 3 1163
执念已碎
执念已碎 2020-12-29 09:56

If I add a new case class, does that mean I need to search through all of the pattern matching code and find out where the new class needs to be handled? I\'ve been learnin

3条回答
  •  忘掉有多难
    2020-12-29 10:41

    Pattern matching is definitely good if you are doing functional programming. In case of OO, there are some cases where it is good. In Cedric's example itself, it depends on how you view the print() method conceptually. Is it a behavior of each Term object? Or is it something outside it? I would say it is outside, and makes sense to do pattern matching. On the other hand if you have an Employee class with various subclasses, it is a poor design choice to do pattern matching on an attribute of it (say name) in the base class.

    Also pattern matching offers an elegant way of unpacking members of a class.

提交回复
热议问题