Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nmeln/ffdd707f974631e1fe125854917b67f4 to your computer and use it in GitHub Desktop.
Save nmeln/ffdd707f974631e1fe125854917b67f4 to your computer and use it in GitHub Desktop.
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 ()
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment