Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3870926
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:46:29+00:00 2026-05-19T21:46:29+00:00

I’m teaching myself Scala and I have sort of a philosophical question. Is pattern

  • 0

I’m teaching myself Scala and I have sort of a philosophical question. Is pattern matching a language feature of Scala, or just a library feature? To put it another way, could I, were I sufficiently skilled, write xmatch, a function that was identical to match in every way except the name? Actually, I think those are two slightly different questions: is matching a library feature, and could it be a library feature?

I was thinking of trying to re-write match, purely as an exercise, but I wanted some assurance it was possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T21:46:29+00:00Added an answer on May 19, 2026 at 9:46 pm

    Pattern matching is a language feature, of which the match statement is only the most notable example. Here are two other commonly-used examples:

    val List(x,y,(z: Int,w: Int)) = List("one","two",(3,4))
    for ((text,i) <- List(("one",1),("two",2))) println(text + " = " + i)
    

    So, no, you can’t do that on your own. The language does not allow you to define new ways to create variables, so these things can only happen with language support.

    The match statement itself uses the pattern-matching variable-creation support inside the language, but otherwise could in principle be implemented as a library feature. However, it would be inefficient in several cases:

    // This is implemented with fast jumps, not slow if-then-else!
    n match {
      case 0 => // Do action 0
      case 1 => // Do action 1
      case 2 => // Do action 2
      case _ => // Do default action
    }
    
    // This is tail recursive, so you won't overflow the stack!
    def recursiveMatch(xs: List[Any]): List[Any] = xs match {
      case (x @ Int) :: rest => recursiveMatch(rest)
      case _ => xs
    }
    

    So, all in all, no, you can’t write pattern matching yourself, and while you could write the match statement, there are advantages in using the existing one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.