Skip to content

Instantly share code, notes, and snippets.

@sorokod
Last active May 2, 2021 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sorokod/c30ccc4b85342144ce887378b37187b7 to your computer and use it in GitHub Desktop.
Save sorokod/c30ccc4b85342144ce887378b37187b7 to your computer and use it in GitHub Desktop.
/**
* A type safe wrapper for comparable IDs
**/
import java.util.UUID
abstract class RefId<T : Comparable<T>> : Comparable<RefId<T>> {
abstract val value: T
override fun compareTo(other: RefId<T>) = value.compareTo(other.value)
}
data class UsrId(override val value: UUID) : RefId<UUID>()
data class Usr(val id: UsrId)
fun main() {
val uuid1 = UUID.randomUUID()
val uuid2 = UUID.randomUUID()
println(UsrId(uuid1) == UsrId(uuid1))
println(UsrId(uuid1) == UsrId(uuid2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment