{{ message }}

Instantly share code, notes, and snippets.

# bentrengrove/Sample.kt

Last active Jan 6, 2021
Sample code demonstrating using Kotlin for type safe unit conversion
 val miles = 1.kilometers.to(Distance.Mile) val kilometers = 1.miles.to(Distance.Kilometer) val total = 2.kilometers + 1.miles + 10.meters + 1.centimeter + 1.millimeter Log.d("MainActivity", "Total distance is \${total.meters.amount} meters") val totalTime = 2.hours + 2.minutes + 2.seconds + 2.milliseconds Log.d("MainActivity", "Total time is \${totalTime.amount} \${totalTime.unit}")
 open class Unit(val name: String, val ratio: Double) { fun convertToBaseUnit(amount: Double) = amount * ratio fun convertFromBaseUnit(amount: Double) = amount / ratio } open class Quantity(val amount: Double, val unit: T) { fun to(unit: T): Quantity { val baseUnit = this.unit.convertToBaseUnit(amount) return Quantity(unit.convertFromBaseUnit(baseUnit), unit) } operator fun plus(quantity: Quantity): Quantity { val converted = quantity.to(this.unit).amount val amount = this.amount + converted return Quantity(amount, this.unit) } operator fun minus(quantity: Quantity): Quantity { val converted = quantity.to(this.unit).amount val amount = this.amount - converted return Quantity(amount, this.unit) } operator fun times(quantity: Quantity): Quantity { val converted = quantity.to(this.unit).amount val amount = this.amount * converted return Quantity(amount, this.unit) } operator fun div(quantity: Quantity): Quantity { val converted = quantity.to(this.unit).amount val amount = this.amount / converted return Quantity(amount, this.unit) } } class Distance(name: String, ratio: Double) : Unit(name, ratio) { companion object Factory { val Mile = Distance("Mile", 1.60934 * 1000.0) val Kilometer = Distance("Kilometer", 1000.0) val Meter = Distance("Meter", 1.0) val Centimeter = Distance("Centimeter", 0.01) val Millimeter = Distance("Millimeter", 0.001) } } val Quantity.miles get() = this.to(Distance.Mile) val Quantity.kilometers get() = this.to(Distance.Kilometer) val Quantity.meters get() = this.to(Distance.Meter) val Quantity.centimeters get() = this.to(Distance.Centimeter) val Quantity.millimeters get() = this.to(Distance.Millimeter) val Number.meters: Quantity get() = Quantity(this.toDouble(), Distance.Meter) val Number.kilometers: Quantity get() = Quantity(this.toDouble(), Distance.Kilometer) val Number.miles: Quantity get() = Quantity(this.toDouble(), Distance.Mile) val Number.centimeter: Quantity get() = Quantity(this.toDouble(), Distance.Centimeter) val Number.millimeter: Quantity get() = Quantity(this.toDouble(), Distance.Millimeter) class Time(name: String, ratio: Double) : Unit(name, ratio) { companion object Factory { val Hour = Time("Hour", TimeUnit.HOURS.toMillis(1L).toDouble()) val Minute = Time("Minute", TimeUnit.MINUTES.toMillis(1L).toDouble()) val Second = Time("Second", TimeUnit.SECONDS.toMillis(1L).toDouble()) val Millisecond = Time("Millisecond", 1.0) } } val Quantity

### ilya-g commented May 23, 2017

 How comes that you multiply Distance by Distance and get Distance in result?