Skip to content

Instantly share code, notes, and snippets.

@AlexRogalskiy
Last active December 20, 2020 10:23
Show Gist options
  • Save AlexRogalskiy/6addfbeab81eaf36de155220d390f4c8 to your computer and use it in GitHub Desktop.
Save AlexRogalskiy/6addfbeab81eaf36de155220d390f4c8 to your computer and use it in GitHub Desktop.
User factory
trait ID
case class SequentialID(id: Int) extends ID
case class OtherID(id: Int) extends ID
case class User(val id: ID, val b: Int, val c: Int)
object UserId {
def unapply(w: User) = w match {
case u @ User(SequentialID(id), _, _) => Some(u, id)
case _ => None
}
}
val u = User(SequentialID(1),2,3)
val y = User(OtherID(1),2,3)
u match {
case UserId(u, id) => id
}
y match {
case UserId(u, id) => u
case _ => y
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment