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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:08:00+00:00 2026-05-11T14:08:00+00:00

In Scala it is possible formulate patterns based on the invididual characters of a

  • 0

In Scala it is possible formulate patterns based on the invididual characters of a string by treating it as a Seq[Char].

An example of this feature is mentioned in A Tour of Scala

This is the example code used there:

object RegExpTest1 extends Application {  def containsScala(x: String): Boolean = {    val z: Seq[Char] = x    z match {       case Seq('s','c','a','l','a', rest @ _*) =>                 println('rest is '+rest)                 true       case Seq(_*) =>                 false    }  } 

}

The problem I have with this is the third line of the snippet:

val z: Seq[Char] = x 

Why is this sort of cast necessary? Shouldn’t a String behave like a Seq[Char] under all circumstances (which would include pattern matching)? However, without this conversion, the code snippet will not work.

  • 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. 2026-05-11T14:08:01+00:00Added an answer on May 11, 2026 at 2:08 pm

    Not 100% sure if this is correct, but my intuition says that without this explicit cast you would pattern match against java.lang.String, which is not what you want.

    The explicit cast forces the Scala compiler to use Predef.stringWrapper implicit conversion; thus, as RichString extends Seq[Char], you are able to do a pattern match as if the string were a sequence of characters.

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

Sidebar

Related Questions

Does anyone know if something like this is possible in Scala: case class Thing(property:String)
Is it possible to extend multiple classes in Scala. For example if I have
Is it possible to create types like e.g. String(20) in scala? The aim would
Is it possible in Scala to create a string, which gets code of a
In Scala, is it possible to get the string representation of a type at
Possible Duplicate: Scala on Android: java.lang.NoSuchMethodError: java.lang.String.isEmpty I've just released a new app on
Is it possible to do something like this in Scala : class MyTest {
Is it possible to emulate a C union structure using Scala? In this case
In Scala it's possible to use the annotation @BeanProperty to automatically generate getters and
Possible Duplicate: What is the rationale behind having companion objects in Scala? Thanks for

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.