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

  • Home
  • SEARCH
  • 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 1082913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:21:54+00:00 2026-05-16T22:21:54+00:00

In Programming in Scala the authors write that Scala’s == function compares value equality

  • 0

In Programming in Scala the authors write that Scala’s == function compares value equality instead of reference equality.

This works as expected on lists:

scala> List(1,2) == List(1,2)
res0: Boolean = true

It doesn’t however work on arrays:

scala> Array(1,2) == Array(1,2)
res1: Boolean = false

The authors recommend to use the sameElements function instead:

scala> Array(1,2).sameElements(Array(1,2))
res2: Boolean = true

As an explanation they write:

While this may seem like an inconsistency, encouraging an explicit test of the equality of two mutable data structures is a conservative approach on the part of the language designers. In the long run, it should save you from unexpected results in your conditionals.

  1. What does this mean? What kind of unexpected results are they talking about? What else could I expect from an array comparison than to return true if the arrays contain the same elements in the same position? Why does the equals function work on List but not on Array?

  2. How can I make the equals function work on arrays?

  • 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-16T22:21:55+00:00Added an answer on May 16, 2026 at 10:21 pm

    It is true that the explanation offered in the book is questionable, but to be fair it was more believable when they wrote it. It’s still true in 2.8, but we have to retrofit different reasoning because as you’ve noticed, all the other collections do element comparisons even if they’re mutable.

    A lot of blood had been shed trying to make Arrays seem like the rest of the collections, but this was a tremendously leaky abstraction and in the end it was impossible. It was determined, correctly I think, that we should go to the other extreme and supply native arrays the way they are, using implicit machinery to enhance their capabilities. Where this most noticeably falls down is toString and equals, because neither of them behaves in a reasonable fashion on Arrays, but we cannot intercept those calls with implicit conversions because they are defined on java.lang.Object. (Conversions only happen when an expression doesn’t type check, and those always type check.)

    So you can pick your explanation, but in the end arrays are treated fundamentally differently by the underlying architecture and there’s no way to paper over that without paying a price somewhere. It’s not a terrible situation, but it is something you have to be aware of.

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

Sidebar

Related Questions

I don't understand why authors said that Code Listing 9.1 from Programming in Scala
Programming In Scala explains that tuples' _N numbers are one-based, instead of zero-based, because
From Programming in Scala ,we know that foreach is a high-order function which takes
In Scala (2.7), if I have this function: def foo(args: Array[String]) = for (arg
In Chapter 9 of Programming In Scala, there is an example method like this:
From reading parts of the Programming in Scala book, I realize that Scala can
I read in Programming in Scala section 23.5 that map, flatMap and filter operations
Since a week I'm reading Programming in Scala. The authors introduce elements of the
In Programming in scala it is stated that lists are immutable and arrays are
I'm reading through Programming in Scala. It says: You can redefine the behavior of

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.