xxxxxxxxxx
object ImplicitConversionExample extends App {
// Define a case class representing a temperature in Celsius
case class Celsius(value: Double)
// Define an implicit conversion from Celsius to Fahrenheit
implicit def celsiusToFahrenheit(celsius: Celsius): Fahrenheit = {
Fahrenheit(celsius.value * 9 / 5 + 32)
}
// Define a case class representing a temperature in Fahrenheit
case class Fahrenheit(value: Double)
// Function that accepts a temperature in Fahrenheit
def printFahrenheitTemperature(fahrenheit: Fahrenheit): Unit = {
println(s"Temperature in Fahrenheit: ${fahrenheit.value} °F")
}
// Implicitly convert a Celsius temperature to Fahrenheit
val celsiusTemperature = Celsius(25)
printFahrenheitTemperature(celsiusTemperature) // Automatically converted to Fahrenheit
}