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

+1 for immutable varargs

4 replies
wookietreiber
Joined: 2011-04-24,
User offline. Last seen 42 years 45 weeks ago.

hi

initially @ scala-user: "case class with varargs not immutable" [1]

scala emphasizes immutability - why is a varargs a WrappedArray instead of
some kind of immutable Seq? (java interop.?)

best regards
wookietreiber

[1] http://osdir.com/ml/scala/2011-08/msg00456.html

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: +1 for immutable varargs
Java's varargs are arrays, so by implementing it as a WrappedArray by default, one can almost-seamlessly (at least on the Scala side) and efficiently interoperate with Java.

You can use something else by building the collection yourself.  Try this in the REPL:

  def varg(x: Int*) = x
  varg(List(1): _*)

You'll see that the list went straight through.  Int* is essentially typed as Seq[Int] on the Scala side, so anything that conforms is fair game.  (And it is an immutable view of the collection, since Seq doesn't have an update method.)

You can break things if you try hard enough, of course, or if you don't realize that : _* means "use this collection for the varargs", which has consequences if the collection is mutable _and_ you have a copy of it handy.

  --Rex

On Wed, Aug 17, 2011 at 10:31 AM, wookietreiber <kizkizzbangbang [at] googlemail [dot] com> wrote:
hi

initially @ scala-user: "case class with varargs not immutable" [1]

scala emphasizes immutability - why is a varargs a WrappedArray instead of
some kind of immutable Seq? (java interop.?)

best regards
wookietreiber

[1] http://osdir.com/ml/scala/2011-08/msg00456.html


wookietreiber
Joined: 2011-04-24,
User offline. Last seen 42 years 45 weeks ago.
Re: +1 for immutable varargs

On Wed, Aug 17, 2011 at 11:38:05AM -0400, Rex Kerr wrote:
> Java's varargs are arrays, so by implementing it as a WrappedArray by default,
> one can almost-seamlessly (at least on the Scala side) and efficiently
> interoperate with Java.
>
> You can use something else by building the collection yourself. Try this in
> the REPL:
>
> def varg(x: Int*) = x
> varg(List(1): _*)
>
> You'll see that the list went straight through. Int* is essentially typed as
> Seq[Int] on the Scala side, so anything that conforms is fair game. (And it is
> an immutable view of the collection, since Seq doesn't have an update method.)
>

in the end I don't want to write

varg(List(1,2,3): _*)

but

varg(1,2,3)

because its much more concise, which is one thing of what scala is about

best regards
wookietreiber

> You can break things if you try hard enough, of course, or if you don't realize
> that : _* means "use this collection for the varargs", which has consequences
> if the collection is mutable _and_ you have a copy of it handy.
>
> --Rex
>
> On Wed, Aug 17, 2011 at 10:31 AM, wookietreiber > wrote:
>
> hi
>
> initially @ scala-user: "case class with varargs not immutable" [1]
>
> scala emphasizes immutability - why is a varargs a WrappedArray instead of
> some kind of immutable Seq? (java interop.?)
>
> best regards
> wookietreiber
>
> [1] http://osdir.com/ml/scala/2011-08/msg00456.html
>
>
>

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: +1 for immutable varargs


On Wed, Aug 17, 2011 at 1:24 PM, wookietreiber <kizkizzbangbang [at] googlemail [dot] com> wrote:
On Wed, Aug 17, 2011 at 11:38:05AM -0400, Rex Kerr wrote:
> Java's varargs are arrays, so by implementing it as a WrappedArray by default,
> one can almost-seamlessly (at least on the Scala side) and efficiently
> interoperate with Java.
>
> You can use something else by building the collection yourself.  Try this in
> the REPL:
>
>   def varg(x: Int*) = x
>   varg(List(1): _*)
>
> You'll see that the list went straight through.  Int* is essentially typed as
> Seq[Int] on the Scala side, so anything that conforms is fair game.  (And it is
> an immutable view of the collection, since Seq doesn't have an update method.)
>


in the end I don't want to write

varg(List(1,2,3): _*)

but

varg(1,2,3)

because its much more concise, which is one thing of what scala is about

But you are willing to write
  varg(1,2,3).asInstanceOf[WrappedArray[Int]](0) = 4
just to break your immutability?

  --Rex

wookietreiber
Joined: 2011-04-24,
User offline. Last seen 42 years 45 weeks ago.
Re: +1 for immutable varargs

On Wed, Aug 17, 2011 at 01:32:25PM -0400, Rex Kerr wrote:
>
>
> On Wed, Aug 17, 2011 at 1:24 PM, wookietreiber
> wrote:
>
> On Wed, Aug 17, 2011 at 11:38:05AM -0400, Rex Kerr wrote:
> > Java's varargs are arrays, so by implementing it as a WrappedArray by
> default,
> > one can almost-seamlessly (at least on the Scala side) and efficiently
> > interoperate with Java.
> >
> > You can use something else by building the collection yourself. Try this
> in
> > the REPL:
> >
> > def varg(x: Int*) = x
> > varg(List(1): _*)
> >
> > You'll see that the list went straight through. Int* is essentially
> typed as
> > Seq[Int] on the Scala side, so anything that conforms is fair game. (And
> it is
> > an immutable view of the collection, since Seq doesn't have an update
> method.)
> >
>
>
> in the end I don't want to write
>
> varg(List(1,2,3): _*)
>
> but
>
> varg(1,2,3)
>
> because its much more concise, which is one thing of what scala is about
>
>
> But you are willing to write
> varg(1,2,3).asInstanceOf[WrappedArray[Int]](0) = 4
> just to break your immutability?
>
> --Rex
>

no, I'm not :)

I didn't realize at first that the signature is scala.collection.Seq - the
REPL output confused me: by printing Foo(WrappedArray(...)) I falsely
interpreted that the signature is WrappedArray ... sometimes it is easy to
forget that the REPL prints the runtime type instead of the type of the
signature

thx for the clarification

best regards
wookietreiber

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