Last active
November 21, 2017 19:02
-
-
Save nhaarman/f4b43ab25668a441d91e5f1473505542 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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