Press "Enter" to skip to content

Fun with Scala

Muskan Gupta has started a series on common mistakes in Scala. Part 1 has to do with matching:

In the example, we are matching against the single case at a time. But, what if we want to match against multiple cases at a time? How will we do that?

The solution to that is using “|”(pipe) operator.

Let’s consider a scenario where you want to check if the user input is a String or an Int. If it is String or Int then it’s a perfect match else not. Now, I’ll give you the options for how can we do this and you should pick the correct option.

Part 2 involves unpacking a tuple:

Now, what if we don’t want to access the variables in the way shown in the above picture?

The other way could be doing tuple unpacking. In this, we bind every value in the tuple with a variable/reference. This is done using Pattern Matching internally.

Now, we will look at a situation and I’ll give you some options so that we can figure out the correct way of doing tuple unpacking

These are short, multiple-choice questions along with the explanation.