Last active
December 20, 2020 10:23
-
-
Save AlexRogalskiy/6addfbeab81eaf36de155220d390f4c8 to your computer and use it in GitHub Desktop.
User factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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