Scala: Match expression

Learn
Jul 12, 2021

--

This post is to introduce the match expression in Scala.

This is what a basis match expression looks like

val someValue : Int = 456

println(
someValue match {
case 456 => “found”
}
)

The content within the println statement above is the match expression.

someValue match {
case 456 => “found”
}

There are three parts to it.
- A value
- The match keyword
- And a block enclosed in curly braces with at least one case clause.

Yes, even if you have only a single case clause, you still need the braces.

Match expressions in Scala are much more powerful than switch case statements in Java,the details of which we will get into in later posts.

--

--

Learn
Learn

Written by Learn

On a continuing learning journey..

No responses yet