Sudhir Singh Khanger
07/21/2021, 11:17 AMvar drivingStyle = when (getWeatherConditions()) {
"Sunny" -> "Speedy"
"Foggy", "Rainy" -> "Safely"
"Blizzard" -> "Don't drive!"
else -> "Undefined"
}
And
sealed class Weather
object Rainy : Weather()
object Sunny : Weather()
object Foggy : Weather()
object Blizzard : Weather()
var drivingStyle = when (getWeatherConditions()) {
Sunny -> "Speedy"
Foggy, Rainy -> "Safely"
}
1. What is the benefit of using Sealed classes in this case over const
? Is it expensive to create a Sealed class over a constant? The one benefit I see is that I don't have to add else
branch if I use all classes of the Sealed class.
2. When a derived class of a Sealed class is used in a when block then does when know all the classes which extend a sealed class?mkrussel
07/21/2021, 12:21 PMAlexis Manin
07/22/2021, 6:18 AMdata class Blizzard(val windSpeed) : Weather
object
like in your example).Sudhir Singh Khanger
07/22/2021, 6:13 PM