Polymorphic updates in an immutable class hierarchy

后端 未结 3 798
再見小時候
再見小時候 2021-01-01 15:23

I\'d like to be able to assemble domain objects from traits, according to various properties that the concrete classes might have. When my objects are mutable, this is prett

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 15:32

    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.

提交回复
热议问题