Created
September 18, 2014 18:31
-
-
Save drstevens/2b6a9686881c1183ed55 to your computer and use it in GitHub Desktop.
Reduce boilerplate when not using primitives
This file contains hidden or 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
import scalaz.Id.Id | |
import scalaz.BijectionT._ | |
import scalaz.Lens | |
trait Bijectionz[A, C] { | |
def apply(str: C): A | |
def unapply(a: A): Option[C] | |
val valueLens: Lens[A, C] = bijectionId.lens | |
implicit def bijectionId: Bijection[A, C] = bijection[Id, Id, A, C](a => unapply(a).get, apply) | |
} | |
case class UserId(value: Long) | |
object UserId extends Bijectionz[UserId, Long] | |
case class UserName(value: String) | |
object UserName extensd Bijectionz[UserName, String] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment