How to Generate a UUID in Scala
The idiomatic way to generate a UUID in Scala is UUID.randomUUID() via Java interop — Scala has no separate UUID type of its own, so you call the JVM's java.util.UUID class directly. Assign the result to a val and let Scala's type inference handle the rest. The php uuid page covers UUID v3, v4, and v5 generation with the ramsey/uuid package in PHP projects.
import java.util.UUID
val myUuid: UUID = UUID.randomUUID()
println(myUuid)
// Output: 550e8400-e29b-41d4-a716-446655440000
Explanation
import java.util.UUIDpulls in the JVM's UUID class — no extra library and no build.sbt dependency required.UUID.randomUUID()generates a random UUID (version 4) backed by a cryptographically secure random number generator.val myUuid: UUID = ...shows idiomatic Scala style — an immutablevalwith an explicit type annotation, thoughval myUuid = UUID.randomUUID()works identically since Scala infers the type on its own.println(myUuid)implicitly callstoString, producing the standard 36-character hyphenated format.
Convert a String to a UUID in Scala
Parsing a UUID string in Scala uses the same UUID.fromString() method as Java, which throws an IllegalArgumentException on malformed input rather than returning a safe result. The python uuid generator guide shows how to convert between UUID strings, bytes, and integers for database storage.
import java.util.UUID
import scala.util.Try
val str = "550e8400-e29b-41d4-a716-446655440000"
// Convert the exception-throwing Java API into a safe Option
val maybeUuid: Option[UUID] = Try(UUID.fromString(str)).toOption
maybeUuid match {
case Some(uuid) => println(s"Valid UUID: $uuid")
case None => println("Invalid UUID string")
}
Explanation
UUID.fromString(str)parses a standard 36-character UUID string; called directly it throwsIllegalArgumentExceptionon invalid input, exactly like in Java.import scala.util.Trywraps that exception-throwing call —Try(...)catches the exception and turns it into aSuccessorFailure..toOptionconverts theTryintoOption[UUID]—Some(uuid)on success,Noneon failure — so callers pattern-match instead of catching exceptions.- This
Try(...).toOptionpattern is the idiomatic functional-Scala way to make any throwing Java API safe to call.
Other Options for Generating UUIDs in Scala
java.util.UUID has no native support for UUID version 7, the newer time-sortable format many databases now prefer for primary keys. Since Scala compiles to the JVM, projects that need v7 identifiers reach for the same options available to any Java codebase.
libraryDependencies += "com.github.f4b6a3" % "uuid-creator" % "6.0.0"
import com.github.f4b6a3.uuid.UuidCreator
// Version 7 - Unix timestamp + random, sortable and index-friendly
val uuidV7 = UuidCreator.getTimeOrderedEpoch()
println(uuidV7)
Explanation
uuid-creator(group idcom.github.f4b6a3) is a JVM library that adds v6, v7, and v8 support on top ofjava.util.UUID— being pure Java, it works identically when called from Scala.UuidCreator.getTimeOrderedEpoch()generates a version 7 UUID: a Unix millisecond timestamp in the most significant bits followed by random bits, so IDs sort chronologically.- The returned value is a standard
java.util.UUID, so it interoperates with any Scala or Java code that already expects one. - Teams that can't add a dependency implement RFC 9562's v7 layout manually — timestamp bits plus
SecureRandom— though a maintained library is less error-prone.