-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make units of same type comparable #58
Comments
|
Oh, sorry, I see! You want the actual units to implement comparable. |
I think this depends on the outcome of #42. The design I'm thinking about will likely end up being similar to TemperatureUnit which would make unit being comparable difficult or possibly even invalid for some types. Consider temperature: is a degree Fahrenheit larger, smaller, or the same as a degree Celsius? Likewise, is a degree Celsius larger, smaller or the same as a degree Kelvin? At some degrees, a degree Celsius might be exactly equivalent to a degree Fahrenheit (-40°F/°C), but at much larger magnitudes their scale differs greatly. I think in this case it might be impossible to compare the actual units themselves since they don't scale linearly. Should that prohibit us from implementing Comparable on other units? I don't know. I'm open to ideas under the assumption that they might break in the future. I'd hate to require consumers of this library implement Comparable on their custom units only to find out my naive assumption about |
Are there other quantities where there exist units that start counting below zero? Nothing comes to mind, but I think for any linear units, it's reasonable to either:
The constraint I could see: are there some units that don't scale linearly at all? These would be the only ones that don't have a distinct "size" and therefore unclear how they should sort. If we expect to support such things, then yeah I'd avoid implementing Comparable on the unit. |
I think if we were to offer this, we wouldn't offer is on TemperatureUnit and we'd implement it globally instead of requiring inheritors to implement it. Something like: abstract class LengthUnit : Comparable<LengthUnit> {
abstract fun convertThisToNanometers(quantity: Long): Long
override fun compareTo(other: LengthUnit): Int {
val thisScale = convertThisToNanometers(2L) - convertThisToNanometers(1L)
val otherScale = other.convertThisToNanometers(2L) - other.convertThisToNanometers(1L)
return thisScale.compareTo(otherScale)
}
} |
that's reasonable I think. if you want to prevent inheritors from being able to implement it, I think you'd need to make |
It would be nice for units to implement
Comparable
with other units of the same type. I currently have a function that takesSet<LengthUnit>
and then does some work with the units in order of size. To sort them, I have to dounits.sortedBy { it.nanometerScale }
. If the units supportComparable
, I could just dounits.sorted()
.happy to PR this if it's acceptable
The text was updated successfully, but these errors were encountered: