Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object UserEntityModule {
- case class UserEntityF[F[_], G[_]](id: G[Option[Long]] = None, username: F[String], password: F[String])
- type Id[A] = A
- type Forget[A] = Unit
- // You can also just use Option if you don't care
- // for the domain-specific type
- sealed trait Updatable[+A] {
- def foreach(f : A => Unit): Unit = this match {
- case Update(a) => f(a)
- case Keep => ()
- }
- }
- case class Update[A](a :A) extends Updatable[A]
- case object Keep extends Updatable[Nothing]
- type UserEntity = UserEntityF[Id, Id]
- type UserEntityUpdate = UserEntityF[Updatable, Forget]
- def doCreate(user : UserEntity): Unit = {
- println(s"Created user $user")
- }
- def doUpdate(user : UserEntityUpdate): Unit = {
- user.username.foreach(u => println(s"Update username to be $u"))
- user.password.foreach(p => println(s"Update password to be $p"))
- println(s"I can't do anything with id since it's ${user.id}")
- }
- }
- /*
- USAGE:
- scala> doCreate(UserEntityF[Id, Id](Some(1), "hi", "lol"))
- Created user UserEntityF(Some(1),hi,lol)
- scala> doUpdate(UserEntityF[Updatable, Forget]((), Update("hi there"), Keep))
- Update username to be hi there
- I can't do anything with id since it's ()
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement