DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Constructing Real-Time Analytics: Fundamental Components and Architectural Framework — Part 2
  • File Upload Security and Malware Protection
  • Turbocharge Ab Initio ETL Pipelines: Simple Tweaks for Maximum Performance Boost
  • How Agile Works at Tesla [Video]

Trending

  • Constructing Real-Time Analytics: Fundamental Components and Architectural Framework — Part 2
  • File Upload Security and Malware Protection
  • Turbocharge Ab Initio ETL Pipelines: Simple Tweaks for Maximum Performance Boost
  • How Agile Works at Tesla [Video]
  1. DZone
  2. Coding
  3. Languages
  4. Combating Fallacy in Scala: Part 1

Combating Fallacy in Scala: Part 1

Muskan Gupta user avatar by
Muskan Gupta
·
Oct. 01, 20 · Tutorial
Like (1)
Save
Tweet
Share
703 Views

Join the DZone community and get the full member experience.

Join For Free

Hi everyone, I am going to start a blog series related to Scala. This is going to be a bit different as it focuses on things such as some mistakes that we usually do, unknowingly, and some concepts that we might not know. So, this series is all about exploring Scala and Combating Fallacy in Scala. 

So for the first part, the topic is "Matching with Multiple Conditions"

Matching With Multiple Conditions

Before looking into this, you should have an idea about Pattern Matching. According to Scala Docs "Pattern matching is a mechanism for checking a value against a pattern."

In pattern matching, we try to find a perfect match/the condition that suits the variable/collection/constant against which we are matching.

For example, consider the following:

pattern matching in Scala

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.

Option 1

pattern matching with pipe operator

Option 2

pattern matching option 2


What do you think? Should both of them work or is there any issue with one of them?

Time to Reveal

The correct option is Option 2. But why? 

So the reason is Scala does not allow bound variables when we are using "|"(pipe) operator. In the first option, "value" is a bound variable, and therefore, there will be a compilation error as following:

illegal variable in pattern alternative

If it is important for you to get the value too, you will have to make different cases. In this case, the solution will be as follows:

pattern matching final example


That's it for the Fun Scala: Part 1. I hope this was helpful.

Stay tuned for the next parts.

Scala (programming language) Fallacy

Published at DZone with permission of Muskan Gupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Constructing Real-Time Analytics: Fundamental Components and Architectural Framework — Part 2
  • File Upload Security and Malware Protection
  • Turbocharge Ab Initio ETL Pipelines: Simple Tweaks for Maximum Performance Boost
  • How Agile Works at Tesla [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: