Skip to content

Instantly share code, notes, and snippets.

@matthandlersux
Created February 1, 2014 12:05
Show Gist options
  • Save matthandlersux/8751399 to your computer and use it in GitHub Desktop.
Save matthandlersux/8751399 to your computer and use it in GitHub Desktop.
possible way to make easily updatable case class objects
trait Base[T] {
val id: Option[String]
def copyId(id: String): T
}
case class Test2(id: Option[String], other: String) extends Base[Test2] {
def copyId(id: String) = copy(id=Some(id))
}
def updateId[T <: Base[T]](obj: T, id: String) = obj.copyId(id)
val y = Test2(None, "hmm")
assert(updateId(y, "!!!") == Test2(Some("!!!"), "hmm"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment