I often use UUID.randomUUID()
. Type inferred by kotlin is UUID!
. is there any way to tell kotlin that return type of this specific method is UUID
and is always non null? or do i have to do everywhere UUID.randomUUID()!!
or implement my own method?
If you explicitly declare the types, it should declare them non-nullable instead of as a platform type.
val id1 = UUID.randomUUID() // UUID!
val id2: UUID = UUID.randomUUID() // UUID
I do this with a function to make things a bit easier. By declaring the return type, it has the same effect:
fun generateUUID(): UUID = UUID.randomUUID()
Or as an extension:
fun UUID.next(): UUID = UUID.randomUUID()