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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:54:52+00:00 2026-06-15T07:54:52+00:00

If I call toSeq on an immutable Set collection I get an ArrayBuffer .

  • 0

If I call toSeq on an immutable Set collection I get an ArrayBuffer.

scala> Set(1,2,3).toSeq // returns Seq[Int] = ArrayBuffer(1, 2, 3)

This surprises me. Given Scala’s emphasis on using immutable data structures, I expect to get back an immutable sequence like a Vector or List instead of a mutable ArrayBuffer. The returned ordering of the set elements should of course be undefined, but there doesn’t seem to be any semantic reason why that ordering should also be mutable.

In general, I expect Scala operations to always produce immutable results unless I explicitly request a mutable one. This has been my assumption all along, but it is an incorrect one here, and I actually just spent an hour debugging a problem where the unexpected presence of an ArrayBuffer led to a runtime error in a match statement. My fix was to change Set(...).toSeq to Set(...).toList, but this feels like a hack because there’s nothing about my application that requires a list in particular at that point.

Is having Set(...).toSeq return a mutable object a flaw in Scala’s implementation, or is there a principle I am misunderstanding here?

This is Scala 2.9.2.

  • 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-06-15T07:54:53+00:00Added an answer on June 15, 2026 at 7:54 am

    Here is the recent thread on whether Seq should mean immutable.Seq.

    Roland Kuhn:

    collection.Seq not having mutators is not at all a valid defense!

    The example of mutable varargs is rather sneaky.

    Recently,

    scala> Set(1,2,3)
    res0: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
    
    scala> res0.toSeq
    res1: Seq[Int] = ArrayBuffer(1, 2, 3)
    
    scala> res0.to[collection.immutable.Seq]
    res2: scala.collection.immutable.Seq[Int] = Vector(1, 2, 3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Call this from a QWidget: Parser->xmlParser(_layout, xmlPath, comingFrom, mapCount); , but get two No
I call a function like this: call_facebook(103138046395999, samåkning.se, http://www.facebook.com/samakning, page); call_facebook(16089699074, Jag ska köra
I call this function from with my code-behind: DeleteFile(Server.MapPath(/) + sitemap_index.xml) Public Shared Function
I call an URL that returns me JSON (I use JSONKit). I convert it
I call this a flash meeting, but maybe there is another more appropriate name.
call to split on a variable causes a Object doesn't support this property or
What call can I use to get the MAC address of the WAP I'm
I call a service which returns a gzipped file. I have the data as
The call to System.currentTimeMillis() returns always 0 on Motorola Droid, Android 2.0. There is
I'm new to Scala, and I'm struggling to understand why I sometimes don't get

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.