This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Why Seq is not scala.collection.immutable.Seq

6 replies
gerferra
Joined: 2009-08-26,
User offline. Last seen 1 year 38 weeks ago.
Hi,
Without importing anything, Set refers to scala.collection.immutable.Set and Map to scala.collection.immutable.Map. Why Seq refers to scala.collection.Seq?
Welcome to Scala version 2.8.0.r22899-b20100902020112 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20). Type in expressions to have them evaluated.Type :help for more information.
scala> classOf[Seq[_]]res0: java.lang.Class[Seq[_]] = interface scala.collection.Seq
scala> classOf[Set[_]]res1: java.lang.Class[Set[_]] = interface scala.collection.immutable.Set
scala> classOf[Map[_,_]]res2: java.lang.Class[Map[_, _]] = interface scala.collection.immutable.Map

Regards,Germán
Lex
Joined: 2010-02-28,
User offline. Last seen 42 years 45 weeks ago.
Re: Why Seq is not scala.collection.immutable.Seq
Java arrays are wrapped into WrappedArray to integrate with scala collection API. Arrays are used very often and WrappedArray is "mutable.Seq", so it makes sense for "Seq" to be the common supertype "scala.collection.Seq" by default.


On Thu, Sep 23, 2010 at 4:59 PM, Germán Ferrari <german.ferrari@gmail.com> wrote:
Hi,
Without importing anything, Set refers to scala.collection.immutable.Set and Map to scala.collection.immutable.Map. Why Seq refers to scala.collection.Seq?
Welcome to Scala version 2.8.0.r22899-b20100902020112 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20). Type in expressions to have them evaluated.Type :help for more information.
scala> classOf[Seq[_]]res0: java.lang.Class[Seq[_]] = interface scala.collection.Seq
scala> classOf[Set[_]]res1: java.lang.Class[Set[_]] = interface scala.collection.immutable.Set
scala> classOf[Map[_,_]]res2: java.lang.Class[Map[_, _]] = interface scala.collection.immutable.Map

Regards,Germán

gerferra
Joined: 2009-08-26,
User offline. Last seen 1 year 38 weeks ago.
Re: Why Seq is not scala.collection.immutable.Seq
On Thu, Sep 23, 2010 at 6:33 PM, Lex <lexn82@gmail.com> wrote:
Java arrays are wrapped into WrappedArray to integrate with scala collection API. Arrays are used very often and WrappedArray is "mutable.Seq", so it makes sense for "Seq" to be the common supertype "scala.collection.Seq" by default.

This doesn't make sense to me. If I want a mutable Seq I'll use mutable.Seq as I have to do with Map and Set. Is there any documentation about this decision? 

gerferra
Joined: 2009-08-26,
User offline. Last seen 1 year 38 weeks ago.
Re: Why Seq is not scala.collection.immutable.Seq
Hi,
Another (somewhat related) questions.
I've noticed that the default "IndexedSeq" is "collection.IndexedSeq" (like in "Seq") but the type of  "toIndexedSeq" is "immutable.IndexedSeq". 
Is this an error or is on purpose? maybe "IndexedSeq" should be by default  "immutable.IndexedSeq"?
Thinking (erroneously) that "Seq" was by default "immutable.Seq", I was using it as the default type for the seq-like parameters of my methods. Now I'm thinking in change all source files using "Seq" to assure that "immutable.Seq" it's what is in scope. 
What type do you use in such cases? "immutable.Seq" or anything else?
Sorry if this has been discussed previously. Any reference to the discussions regarding the default "Seq" and "IndexedSeq" and/or the return type of "toSeq" and "toIndexedSeq", is very welcome.

Many thanks.
Regards,Germán



On Thu, Sep 23, 2010 at 7:17 PM, Germán Ferrari <german.ferrari@gmail.com> wrote:
On Thu, Sep 23, 2010 at 6:33 PM, Lex <lexn82@gmail.com> wrote:
Java arrays are wrapped into WrappedArray to integrate with scala collection API. Arrays are used very often and WrappedArray is "mutable.Seq", so it makes sense for "Seq" to be the common supertype "scala.collection.Seq" by default.

This doesn't make sense to me. If I want a mutable Seq I'll use mutable.Seq as I have to do with Map and Set. Is there any documentation about this decision? 


hseeberger
Joined: 2008-12-27,
User offline. Last seen 1 year 25 weeks ago.
Re: Why Seq is not scala.collection.immutable.Seq
Wow, I did not know that!I agree: That's very confusing!
Heiko

On 23 September 2010 22:59, Germán Ferrari <german.ferrari@gmail.com> wrote:
Hi,
Without importing anything, Set refers to scala.collection.immutable.Set and Map to scala.collection.immutable.Map. Why Seq refers to scala.collection.Seq?
Welcome to Scala version 2.8.0.r22899-b20100902020112 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20). Type in expressions to have them evaluated.Type :help for more information.
scala> classOf[Seq[_]]res0: java.lang.Class[Seq[_]] = interface scala.collection.Seq
scala> classOf[Set[_]]res1: java.lang.Class[Set[_]] = interface scala.collection.immutable.Set
scala> classOf[Map[_,_]]res2: java.lang.Class[Map[_, _]] = interface scala.collection.immutable.Map

Regards,Germán



--
Heiko Seeberger

Company: weiglewilczek.com
Blog: heikoseeberger.name
Follow me: twitter.com/hseeberger
OSGi on Scala: scalamodules.org
Lift, the simply functional web framework: liftweb.net
Akka - Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Actors: akkasource.org
ijuma
Joined: 2008-08-20,
User offline. Last seen 22 weeks 2 days ago.
Re: Why Seq is not scala.collection.immutable.Seq
On Fri, Sep 24, 2010 at 9:56 PM, Germán Ferrari <german.ferrari@gmail.com> wrote:
I've noticed that the default "IndexedSeq" is "collection.IndexedSeq" (like in "Seq") but the type of  "toIndexedSeq" is "immutable.IndexedSeq".

It's even more confusing than that, if you create an instance of collection.IndexedSeq or collection.Seq, you get something from collection.immutable:
package scala.collection object IndexedSeq extends SeqFactory[IndexedSeq] {  ...  def newBuilder[A]: Builder[A, IndexedSeq[A]] = immutable.IndexedSeq.newBuilder[A]}
package scala.collection object Seq extends SeqFactory[Seq] {  ...  def newBuilder[A]: Builder[A, Seq[A]] = immutable.Seq.newBuilder[A]}
Best,Ismael
gerferra
Joined: 2009-08-26,
User offline. Last seen 1 year 38 weeks ago.
Re: Why Seq is not scala.collection.immutable.Seq
On Sat, Sep 25, 2010 at 6:15 AM, Ismael Juma <mlists@juma.me.uk> wrote:
It's even more confusing than that, if you create an instance of collection.IndexedSeq or collection.Seq, you get something from collection.immutable:
 I see this more as a mitigation of the "problem" than a problem per se. I prefer that the default implementation of collection.Seq/IndexedSeq (returned by the apply method in the companion) be immutable. 

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland