June 2018
Intermediate to advanced
310 pages
6h 32m
English
The concept of pattern matching is like switch/case on steroids for someone who comes from Java. We've already seen how when expression can be used, in Chapter 1, Getting Started with Kotlin, so let's briefly discuss why this concept is important for the functional paradigm.
As you may know, switch in Java accepts only some primitive types, strings, or enums.
Consider the following code in Java:
class Cat implements Animal { public String purr() { return "Purr-purr"; }}class Dog implements Animal { public String bark() { return "Bark-bark"; }
}interface Animal {}
If we were to decide which of the functions to call, we would need something like this:
public String getSound(Animal animal) { String sound = null; if (animal ...
Read now
Unlock full access