是否可以包裝一個案例類的複製方法,例如下面我想 總是設置複製案例類時的時間戳,而沒有手動定義 包裝的所有參數方法?Scala包裝案例類複製方法
case class NodeUpdate(val ip: String,
val actor: Option[ActorRef] = None,
val status: Status = Offline,
val asOf: Long = System.currentTimeMillis) {
// Error type mismatch; found : Seq[Any] required: String
def stampedCopy(values: Any*) = copy(values, asOf = System.currentTimeMillis)
// This works but needs to be updated for each modification
// So I would prefer to use something like what I have above
def stampedCopy2(ip: String = ip, actor: Option[ActorRef] = actor, status: Status = status) =
copy(ip, actor, status, System.currentTimeMillis)
}
編輯:快速跟進,以防有人知道 - 將Akka ActorRef放入案例班一般安全嗎?他們是否穩定不變的對象?
我該如何使用加蓋的班級?我沒有看到'Stamped.copy'如何做和'NodeUpdate.copy'不同的東西' – Hamy 2014-09-24 19:07:34
您可能希望爲'NodeUpdate'添加'stamped'方法。在創建一個'NodeUpdate'的實例後,你可以調用'stamped'來創建一個'Stamped'的實例:'def stamped = Stamped(this)'' – 2014-09-24 20:12:00