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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:19:58+00:00 2026-05-26T12:19:58+00:00

looking through the Scala code, the convenient array creation syntax is achieved by adding

  • 0

looking through the Scala code, the convenient array creation syntax is achieved by adding an apply method to object Array. At first, I thought this was achieved somehow through case classes because you can run the following, but this does not seem to be the case:

Array(1,2,3) match { case Array(a, b, c) => a + b + c }

I know that I also need to look at WrappedArray and all the superclasses, but I can’t figure out how scala achieves this matching on Arrays (and I need to become more familiar with the scala collections class hierarchy). It certainly doesn’t work with a run-of-the-mill class.

scala>  class A(val x: Int)
scala>  new A(4) match { case A(x) => x }
<console>:9: error: not found: value A
              new A(4) match { case A(x) => x }
                                    ^
<console>:9: error: not found: value x
              new A(4) match { case A(x) => x }

How do they get this to work with Array?

  • 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-26T12:19:59+00:00Added an answer on May 26, 2026 at 12:19 pm

    You can pattern match with this syntax on any class so long as you have an object with with an unapply or unapplySeq (in the case of varargs) method that returns an Option or Boolean. These are known as extractors. The lines in question from object Array are

      def unapplySeq[T](x: Array[T]): Option[IndexedSeq[T]] =
        if (x == null) None else Some(x.toIndexedSeq) 
    

    In your example you can get it to match using

    class A(val x: Int)
    
    object A {
      def unapply(a: A) = Some(a.x)
    }
    

    so now

    scala> new A(4) match { case A(x) => x }
    res1: Int = 4
    

    The Programming In Scala chapter on extractors may be useful.

    For case classes, an unapply method is just one of the methods that is included for free, along with toString, equals, etc.

    Note that the extractor doesn’t have to have the same name as the class in question, and it doesn’t have to be defined within an object object. For example, in your case you could equally write

    val xyz = new { def unapply(a: A) = Some(a.x) } //extending java.lang.Object
    
    new A(4) match { case xyz(x) => x }             //Int = 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking through the Scala 2.8 Predef class, I find that there is a method
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
Looking through decimal.py , it uses NotImplemented in many special methods. e.g. class A(object):
I was looking through Mozilla's JS in JS code and came across the snarf
I was looking through the Java documentation, and I came across the clear() method
Looking at scala's Predef object, which is automatically imported, I found the following gem
Looking through some old company code, I came across a for loop that looks
Looking through my server logs, I see that a lot of pages on my
Looking through a few SQL implementations, I noticed that most DBMSs support defining an
I was just looking through some information about Google's protocol buffers data interchange format.

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.