I just released <Measured>. A library that makes u...
# feed
n
I just released Measured. A 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.
Copy code
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
Try it out and provide feedback. (edited)
👍 5
🎖️ 2
h
3rd time is the charm
😅 1
a
Interesting. We had this feature on a backlog: https://github.com/mipt-npm/kmath/issues/75. But a separate library is also a good solution. Your current implementation has one flaw: it uses wrapper classes for
Measurement
meaning that you will have boxing performance overhead. My idea was to use inline clases with
Units
passed as a generic and resolved via reflections or comile-time resolution. It seems that that feature could be added without breaking existing code. We can discuss it in #science .
k
Wait why are you multiplying by
1000
before converting to kilometres and miles?
n
Good catch. The docs had some old cruft. Fixed.
r
looks great