Advertisement
Guest User

Untitled

a guest
May 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. object UserEntityModule {
  2. case class UserEntityF[F[_], G[_]](id: G[Option[Long]] = None, username: F[String], password: F[String])
  3.  
  4. type Id[A] = A
  5. type Forget[A] = Unit
  6.  
  7. // You can also just use Option if you don't care
  8. // for the domain-specific type
  9. sealed trait Updatable[+A] {
  10. def foreach(f : A => Unit): Unit = this match {
  11. case Update(a) => f(a)
  12. case Keep => ()
  13. }
  14. }
  15. case class Update[A](a :A) extends Updatable[A]
  16. case object Keep extends Updatable[Nothing]
  17.  
  18.  
  19. type UserEntity = UserEntityF[Id, Id]
  20. type UserEntityUpdate = UserEntityF[Updatable, Forget]
  21.  
  22.  
  23. def doCreate(user : UserEntity): Unit = {
  24. println(s"Created user $user")
  25. }
  26.  
  27. def doUpdate(user : UserEntityUpdate): Unit = {
  28. user.username.foreach(u => println(s"Update username to be $u"))
  29. user.password.foreach(p => println(s"Update password to be $p"))
  30. println(s"I can't do anything with id since it's ${user.id}")
  31. }
  32. }
  33.  
  34. /*
  35. USAGE:
  36.  
  37. scala> doCreate(UserEntityF[Id, Id](Some(1), "hi", "lol"))
  38. Created user UserEntityF(Some(1),hi,lol)
  39.  
  40. scala> doUpdate(UserEntityF[Updatable, Forget]((), Update("hi there"), Keep))
  41. Update username to be hi there
  42. I can't do anything with id since it's ()
  43. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement