Jérémy CROS
01/05/2023, 2:27 PMvalues classes
right now. We’re using Location objects a lot in our app and I thought they would be a very good fit to replace longitude: Double
et latitude: Double
.
After a couple hours trying to slowly replace them in our codebase, looks like they are a lot less convenient than I expected...
In particular, is there no simple way to do some basic math operations on them out of the box?
For exemple, doing some area bounds computation like:
val averageLatitude =
(area.bottomLeft.latitude + area.topLeft.latitude + area.bottomRight.latitude) / 3
Looks like I have 2 options right now:
• call .value
on all latitude objects
• follow the suggestion and create a Latitude.plus
member function
Am I missing anything?Kristian Nedrevold
01/05/2023, 2:53 PMinterface Coord<T> {
val value: T
}
operator fun Coord<Double>.plus(other: Coord<Double>) = this.value + other.value
@JvmInline
value class Lat(override val value: Double) : Coord<Double>
@JvmInline
value class Long(override val value: Double) : Coord<Double>
fun main() {
val lat = Lat(10.0)
val long = Long(10.0)
val sum = lat + long
}
Would be ok?Jérémy CROS
01/05/2023, 3:59 PMplus
for both latitude and longitude, thanks for the suggestion 🙂
thinking on giving up on it anyway 😕 the feature looked promising but it’s not quite there yet, just learned that it doesn’t work with room for example 😞Klitos Kyriacou
01/05/2023, 4:05 PMKlitos Kyriacou
01/05/2023, 4:06 PMvalue class Lat(override val value: Double) : Coord<Lat>
or similarJérémy CROS
01/05/2023, 4:51 PMphldavies
01/05/2023, 5:06 PMLat.plus(Lat) -> Double
. That may or may not be desired. By defining twice (should only really need to be written once… and copy/paste/search/replace should get you there quickly) you gain Lat.plus(Lat) -> Lat
- you can also still have Lat.div(Int) -> Double
etc as required. One option to avoid the pain of writing this might be to look into KSP to generate some math operations automagically.