Java UUID's aren't exceptionally safe. Operations throw and are not referentially transparent. We can fix that.
Currently this is the minimal implementation that keeps me content.
To use fuuid in an existing SBT project with Scala 2.11 or a later version, add the following dependency to your
build.sbt:
libraryDependencies += "io.chrisdavenport" %% "fuuid" % "<version>"import io.chrisdavenport.fuuid.FUUID
import cats.effect.IO
// We place it in IO because this requires a Sync constraint
val create: IO[FUUID] = FUUID.randomFUUID[IO]
val fromString : Either[IllegalArgumentException, FUUID] = FUUID.fromString("d6faceab-4193-4508-86ca-e1561d38fea6")
val failsReferentiallyTransparently : Either[IllegalArgumentException, FUUID] = FUUID.fromString("Not a UUID")
// Will be a Left
// For some syntax improvements
import cats.implicits._
// Uses cats Eq
val equalToItself : IO[Boolean] = for {
fuuid <- FUUID.randomFUUID[IO]
} yield fuuid === fuiid
equalToItself.unsafeRunSync
// true
// Uses cats Order
val laterGreaterThanEarlier : IO[Boolean] = for {
fuuid1 <- FUUID.randomFUUID[IO]
fuuid2 <- FUUID.randomFUUID[IO]
} yield fuuid2 > fuuid1
// true