Const val multiplication

In many places I want to express constants in seconds (and compile them as ms). Is there something like C++ units (aka User literals) in Kotlin?

const val MS_IN_SECOND = 1000
const val MS_IN_SECOND_L = MS_IN_SECOND.toLong()
fun Int.longSeconds() =  MS_IN_SECOND_L * this

const val TIME_BETWEEN_EXECUTIONS = 5.longSeconds() // ERROR: Const 'val' initializer should be a constant value

Do you really need the “const” modifier? I think the only real use case for a compile-time-constant value is if you want to use the value in an anotation.

1 Like

Got it. I guess I do too much C++ :slight_smile:

1 Like

FYI, there are some libraries for Kotlin that support units of measure that may be of use to you. E.g.: