Hullaballoonatic
11/23/2019, 3:42 AMval distance = 512.m
val time = 70.s
val speed = distance / time
The compiler only recognizes speed
to be of type Quantity<*>
instead of Quantity<Velocity>
, even though in the code this operation is specifically instantiating a quantity of velocity. Is there some kind of magic in Arrow that could ensure this without me having to hard code every compound quantity conversion?
I'm almost certain at this point that this is impossible to do.kunalsheth
11/23/2019, 6:50 AMsimon.vergauwen
11/23/2019, 9:45 AMpakoito
11/23/2019, 12:05 PMinline Measured<T>(Val v: Double)
. Then you make several phantom types such as class Meters private constructor()
and definie addition just for the same matching type
operator fun <T> Measured<T>.plus(o: Measured<T>, AP: Applicative<ForMeasured>) =
AP.just(v + o.v)
pakoito
11/23/2019, 12:06 PMpakoito
11/23/2019, 12:09 PMpakoito
11/23/2019, 12:09 PMpakoito
11/23/2019, 12:10 PMoperator fun Measured<Meter>.plus(o: Measured<Kilometer>, AP: Applicative<ForMeasured>): Measured<Meter> =
AP.just(v + o.v *1000)
pakoito
11/23/2019, 12:11 PMpakoito
11/23/2019, 12:12 PMpakoito
11/23/2019, 12:13 PMpakoito
11/23/2019, 12:15 PMpakoito
11/23/2019, 12:16 PMclass Measured<T> private constructor (val v: Double) {
companion object {
fun meters (d: Double): Measured<Meters> = ...
}
}
pakoito
11/23/2019, 12:40 PMSpeed<T>
and Time<U>
and Velocity<T, U>
you can get away with some of itpakoito
11/23/2019, 12:40 PMpakoito
11/23/2019, 12:43 PMsealed class Measure<T>(…) {
class Velocity<U>(…): Measure<Composed<T, U>>
}
class Composed<T,U> private constructor()
that way you can express composed typespakoito
11/23/2019, 12:44 PMpakoito
11/23/2019, 1:01 PMpakoito
11/23/2019, 1:02 PMoperator fun Measurement.Distance.div(other: Measurement.Time): Int
oooh fancy 😄 niceraulraja
11/23/2019, 1:18 PMRyan Benasutti
11/23/2019, 3:09 PMraulraja
11/23/2019, 4:51 PMraulraja
11/23/2019, 4:51 PMraulraja
11/23/2019, 4:51 PMraulraja
11/23/2019, 4:52 PMHullaballoonatic
11/23/2019, 9:47 PMLᵃMᵇTᶜ...
In the example in OP, when we divide distance (L¹)
by time (T¹)
our resulting dimension is L¹T⁻¹
which corresponds to the quantity Velocity.
So essentially I'm trying to write a single function operator fun <A: Quantity<A>, B: Quantity<B>, R: Quantity<R>> A.times(other: B): R
such that the compiler can infer the return type by looking up whatever the resultant dimension maps toHullaballoonatic
11/23/2019, 9:50 PMkunalsheth
11/23/2019, 9:52 PMkunalsheth
11/23/2019, 9:53 PMHullaballoonatic
11/23/2019, 9:56 PMJoulesPerMoleKelvin
and the like.
in a perfect world, the code could recognize a quantity of any dimension, considering there are potentially infinitekunalsheth
11/23/2019, 9:58 PMkunalsheth
11/23/2019, 9:59 PMkunalsheth
11/23/2019, 10:00 PMHullaballoonatic
11/23/2019, 10:00 PMkunalsheth
11/23/2019, 10:02 PMOne.plus(x: Two)
doesn’t exist until the user calls it.Hullaballoonatic
11/23/2019, 10:02 PMHullaballoonatic
11/23/2019, 10:04 PMkunalsheth
11/23/2019, 10:04 PMHullaballoonatic
11/23/2019, 10:12 PM3.kilo(Metre)
instead of 3.kilo.Metre
? Not judging. Curiousraulraja
11/23/2019, 10:21 PMraulraja
11/23/2019, 10:21 PMraulraja
11/23/2019, 10:21 PMraulraja
11/23/2019, 10:22 PMraulraja
11/23/2019, 10:22 PMHullaballoonatic
11/23/2019, 10:22 PMraulraja
11/23/2019, 10:23 PMraulraja
11/23/2019, 10:24 PMraulraja
11/23/2019, 10:24 PMraulraja
11/23/2019, 10:25 PM