https://kotlinlang.org logo
#feed
Title
# feed
n

Nick

06/14/2020, 3:39 AM
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

Hamza

06/14/2020, 3:39 AM
3rd time is the charm
😅 1
a

altavir

06/14/2020, 6:12 AM
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

Kroppeb

06/14/2020, 7:43 AM
Wait why are you multiplying by
1000
before converting to kilometres and miles?
n

Nick

06/14/2020, 7:56 AM
Good catch. The docs had some old cruft. Fixed.
r

raulraja

06/15/2020, 12:54 PM
looks great
2 Views