Nick
06/18/2024, 4:20 AMval 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
Piotr Krzemiński
06/18/2024, 6:57 AMNick
06/18/2024, 8:12 AMMeasure<UnitsRatio<Length, Square<Time>>>
. or create a typealias for the unit and take a Measure of that:
typealias Acceleration = UnitsRatio<Length, Square<Time>>
fun foo(acceleration: Measure<Acceleration>)
Measured will just create these kinds of units automatically as you multiply and divide other units. it handles canceling numerators and denominators as well. of course there are still limits to how far this is taken in the library. but these gaps can easily be added via extensions. custom units are also fully supported. and they participate in the same dimensional logic as any other unit. you can also easily add new types to an existing unit. the docs show how to do both.Piotr Krzemiński
06/18/2024, 8:13 AMStefan Oltmann
06/18/2024, 8:24 AMas
so that it must not be escaped? 🤔Nick
06/18/2024, 8:29 AMKlitos Kyriacou
06/18/2024, 8:29 AMin
that also has to be escaped. I'm not sure I can think of a better word for them. By the way, there's a section titled "Complex units" which might possibly be confusing as it does not involve complex numbers.Piotr Krzemiński
06/18/2024, 8:30 AMto
look weird? no need to escapePiotr Krzemiński
06/18/2024, 8:31 AMPair
(docs, usually used in the context of maps), it's auto-importedStefan Oltmann
06/18/2024, 9:36 AMto
you would need the full qualified name.
I'm thinking if something presentedAs
(or another prefix) would help here.
I don't like the escapes.Piotr Krzemiński
06/18/2024, 9:38 AMKlitos Kyriacou
06/18/2024, 9:42 AMStefan Oltmann
06/18/2024, 9:57 AMuli
06/18/2024, 10:17 AMNick
06/18/2024, 3:37 PMLength/Time
is not the same as Time/Length
etc.? the current approach produces 2 different Kotlin types for these and lets you write functions that require specific types like: Measure<UnitsRatio<Length, Time>>
, aka velocity.uli
06/18/2024, 3:52 PMLength*Time
and Time*Length
would still get different types, even when there suffixes and exponents could be matched at runtime.
This sounds like an interesting challenge. And you are probably right, that the kotlin type system can not solve that. At least I have no idea how to force types to be fully ordered