Skip to content

Instantly share code, notes, and snippets.

@nhaarman
Last active November 21, 2017 19:02
Show Gist options
  • Save nhaarman/f4b43ab25668a441d91e5f1473505542 to your computer and use it in GitHub Desktop.
Save nhaarman/f4b43ab25668a441d91e5f1473505542 to your computer and use it in GitHub Desktop.
sealed class Distance : Comparable<Distance> {
data class DistanceMeters(val value: Float) : Distance()
data class DistanceFeet(val value: Float) : Distance()
fun toMeters() = when (this) {
is DistanceMeters -> this
is DistanceFeet -> DistanceMeters(value * .3048f)
}
fun toFeet() = when (this) {
is DistanceMeters -> DistanceFeet(value / .3048f)
is DistanceFeet -> this
}
override fun compareTo(other: Distance): Int {
return this.toMeters().value.compareTo(other.toMeters().value)
}
}
fun Location.distanceMetersTo(other: Location) = DistanceMeters(distanceTo(other))
val Int.meters get() = DistanceMeters(this.toFloat())
fun foo(location1: Location, location2: Location) {
if (location1.distanceMetersTo(location2) < 100.meters) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment