DEV Community

Nicholas Eddy
Nicholas Eddy

Posted on

Measured - Intuitive, Type-safe Units

Measured is a multi-platform, Kotlin library that makes units a lot simpler and intuitive to work with. It uses the compiler to enforce correctness and lets you combine units into more complex ones using math operators. It is also extensible, making it easy to define your own units.

val velocity     = 5 * meters / seconds
val acceleration = 9 * meters / (seconds * seconds)
val time         = 1 * minutes

//  d = vt + ½at²
val distance     = velocity * time + 1.0/2 * acceleration * time * time

println(distance                ) // 16500 m
println(distance `as` kilometers) // 16.5 km
println(distance `as` miles     ) // 10.25262467191601 mi

println(5 * miles / hours `as` meters / seconds) // 2.2352 m/s
Enter fullscreen mode Exit fullscreen mode

Give it a try. And leave a star or any other feedback.

Top comments (0)