-
-
Save abelbryo/a5270772f43cfe0fb45646a687f1c599 to your computer and use it in GitHub Desktop.
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
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