Kotlin inline class — a use case

One of the use cases why an inline class is useful.

Photo by Liao Je Wei on Unsplash
typealias Fahrenheit = Double
typealias Celsius = Double
fun convert(value: Fahrenheit): Celsius = TODO()
fun convert(value: Celsius): Fahrenheit = TODO()
return when(value) {
is Fahrenheit -> TODO()
is Celsius -> TODO()
}
// The above is actually resulting in this:
return when(value) {
is Double -> TODO()
is Double -> TODO()
}
fun Fahrenheit.toCelsius(): Celsius = TODO()
fun Celsius.toFahrenheit(): Fahrenheit = TODO()
val temperature: Fahrenheit = 0.0
val temperature2 = temperature.toFahrenheit()
inline class Fahrenheit(val degree:Double)
inline class Celsius(val degree:Double)
fun convert(degree: Fahrenheit): Celsius = TODO()
fun convert(degree: Celsius): Fahrenheit = TODO()
fun Fahrenheit.toCelsius(): Celsius = TODO()
fun Celsius.toFahrenheit(): Fahrenheit = TODO()
val temperature = Fahrenheit(0.0)
val c = temperature.toCelsius() // can't call toFahrenheit here

Lead Developer at Qvik, Coach, Agile Thinker, GDG Lead.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store