开发者

'copy' for non-case classes?

When I'm designing immutable objects, case classes are incredibly handy because of the auto-generated copy method.

But case classes have their own problems: th开发者_StackOverflow中文版ey shouldn't be inherited from and they give you an extractor even if you don't want one.

So sometimes I have to use a garden-variety Scala class. The problem is that then I have to write my own immutable API, which can be pretty repetitive:

class Debt(principalBalance: Double, name: String, endDate: LocalDate) {
  def withNewPrincipalBalance(bal: Double) = new Debt(bal, name, endDate)
}

Is there a more scalable way to do this? Is there a compiler plugin I can use?


I don't know about a compiler plugin, but you can define a copy method just like the one generated in case classes using named arguments in combination with default arguments.

class Debt(principalBalance: Double, name: String, endDate: LocalDate) {
  def copy(principalBalance: Double = principalBalance,
           name: String = name,
           endDate: LocalDate = endDate) = new Debt(principalBalance, name, endDate)
}

This is not as repetitive as separate methods for each property (withNewPrincipalBalance) and makes it possible to disallow changes of certain values (for example the creation date).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜