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 6731971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:35:35+00:00 2026-05-26T10:35:35+00:00

As a follow-up to this question Here is some code that compiles and runs

  • 0

As a follow-up to this question

Here is some code that compiles and runs correctly, using captures.

val myString = "ACATCGTAGCTGCTAGCTG"

val nucCap = "([ACTG]+)".r

myString match {
   case nucCap(myNuc) => println("dna:"+myNuc)
   case _ => println("not dna")
}

>scala scalaTest.scala 
dna:ACATCGTAGCTGCTAGCTG

Here is simpler code, without capture, that does not compile.

val myString = "ACATCGTAGCTGCTAGCTG"

val nuc = "[ACGT]+".r

myString match {
     case nuc => println("dna")
     case _ => println("not dna")
}

>scala scalaTest.scala
scalaTest.scala:7: error: unreachable code

Seems like the matching should return a boolean regardless of whether a capture is used.
What is going on here?

  • 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-26T10:35:35+00:00Added an answer on May 26, 2026 at 10:35 am

    In your match block, nuc is a pattern variable and does not refer to the nuc in the enclosing scope. This makes the default case unreachable because the simple pattern nuc will match anything.

    An empty pair of parentheses on nuc will make the syntactic sugar work and call the unapplySeq method on the Regex:

    myString match {
      case nuc() => println("dna")
      case _ => println("not dna")
    }
    

    One way to avoid this pitfall is to rename nuc to Nuc. Starting with an uppercase letter makes it a stable identifier, so that it refers to the Nuc in the enclosing scope, rather than being treated by the compiler as a pattern variable.

    val Nuc = "[ACGT]+".r
    myString match {
      case Nuc => println("dna")
      case _ => println("not dna")
    }
    

    The above will print "not dna", because here we are simply comparing Nuc to myString, and they are not equal. It’s a bug, but maybe a less confusing one!

    Adding the parentheses will have the desired effect in this case too:

    myString match {
      case Nuc() => println("dna")
      case _ => println("not dna")
    }
    // prints "dna"
    

    By the way, it is not a boolean that is being returned, but an Option[List[String]]:

    scala> nuc.unapplySeq(myString)
    res17: Option[List[String]] = Some(List())
    scala> nucCap.unapplySeq(myString)
    res18: Option[List[String]] = Some(List(ACATCGTAGCTGCTAGCTG))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In follow up to this question , it appears that some numbers cannot be
This question is sort of a follow-up to my original question here . Let's
This is a follow-up to my other question here: How to read vfat attributes
This is a follow-on question from the one I asked here . Can constraints
Follow up to this question . I have the following code: string[] names =
This is a follow up to the question asked here: Groovy parsing text file
This question is a follow up question for this question . If some browsers
All, this is a follow up from a previous question here: C# formatting external
This is my first question here, so I hope that I am providing enough
All, This is a follow up for my question here . My setup: Visual

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.