且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

不可变类层次结构中的多态更新

更新时间:2023-12-04 19:34:40

您希望避免M具体类中的N更新方法。
然而痛苦我认为这是不可能的。您将需要访问复制方法或至少每个具体类的构造函数。它们都不能被抽象化,如下所述: Case class copy()方法抽象
所以最后你总是会得到N x M'样板'代码。

You are hoping to avoid the N update methods in M concrete classes. However painful I think this is just not possible. You will need access to the copy method or at least the constructor of every concrete class. Neither of them can be abstracted as is also discussed in: Case class copy() method abstraction So in the end you will always end up with the N x M 'boilerplate' code.