Case-modddels pattern matching
Let's continue with our Weather
example (find it here).
The super-sealed classes have additional "special" pattern matching methods that allow you to map between the different modddels. In our example, these methods are : mapWeather
, maybeMapWeather
and mapOrNullWeather
.
For Example :
In general, these methods are named 'map{ModddelName}', 'maybeMap{ModddelName}' and 'mapOrNull{ModddelName}'.
These methods can be used on all the super-sealed classes :
Weather
Sunny
- Rainy
ValidWeather
ValidSunny
- ValidRainy
InvalidWeather
InvalidSunny
- InvalidRainy
InvalidWeatherValue
InvalidSunnyValue
- InvalidRainyValue
For Example :
Notice how the type of the callback parameters changes according to which super-sealed class the mapWeather
method is called on.
Last updated