Skip to content

Instantly share code, notes, and snippets.

Created May 17, 2017 19:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0112ce98f2b1367ae467b2a1b1ecb6b5 to your computer and use it in GitHub Desktop.
Save anonymous/0112ce98f2b1367ae467b2a1b1ecb6b5 to your computer and use it in GitHub Desktop.

Here's how you can add a type variable to not maintain two parallel-but-similar case classes:

case class UserEntityF[F[_]](id: Option[Long] = None, username: F[String], password: F[String])

type Id[A] = A

// You can also just use Option if you don't care
// for the domain-specific type
sealed trait Updatable[+A]
case class Update[A](a :A) extends Updatable[A]
case object Keep extends Updatable[Nothing]

type UserEntity = UserEntityF[Id]
type UserEntityUpdate = UserEntityF[Updatable]

Here's how you can add another type variable to erase certains fields from the *Update version:

case class UserEntityF[F[_], G[_]](id: G[Option[Long]] = None, username: F[String], password: F[String])

type Id[A] = A
type Forget[A] = ()

// You can also just use Option if you don't care
// for the domain-specific type
sealed trait Updatable[+A]
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment