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

The SLS on Array.copy vs. System.arraycopy

3 replies
milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.

According to the SLS 12.3.4 (p. 140 of the version that Adriaan posted earlier),

System.arraycopy will not work for Scala’s polymorphic arrays because of
their different representation. One should instead use method Array.copy
which is defined in the companion object of class Array.

Presumably System.arraycopy does work for Scala array now (tho' we
might still prefer Array.copy).

Cheers,

Miles

nilskp
Joined: 2009-01-30,
User offline. Last seen 1 year 27 weeks ago.
Re: The SLS on Array.copy vs. System.arraycopy
Does array.clone work in 2.8?

On Tue, Oct 20, 2009 at 5:50 PM, Miles Sabin <miles@milessabin.com> wrote:
According to the SLS 12.3.4 (p. 140 of the version that Adriaan posted earlier),

 System.arraycopy will not work for Scala’s polymorphic arrays because of
 their different representation. One should instead use method Array.copy
 which is defined in the companion object of class Array.

Presumably System.arraycopy does work for Scala array now (tho' we
might still prefer Array.copy).

Cheers,


Miles

--
Miles Sabin
tel: +44 (0)7813 944 528
skype:  milessabin
http://www.chuusai.com/
http://twitter.com/milessabin

Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.
Re: The SLS on Array.copy vs. System.arraycopy

>>>>> "Nils" == Nils Kilden-Pedersen writes:

Nils> Does array.clone work in 2.8?

I don't know if it's a bug or a feature, but in 2.8 toArray copies
arrays:

Welcome to Scala version 2.8.0.r19127-b20091018090027 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_15).

scala> Array(0)
res0: Array[Int] = Array(0)

scala> res0.toArray
res1: Array[Int] = Array(0)

scala> res0(0) = 99

scala> res0
res3: Array[Int] = Array(99)

scala> res1
res4: Array[Int] = Array(0)

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: The SLS on Array.copy vs. System.arraycopy
Which was related to a ticket I opened a while ago, where passing an array as a vararg resulted in changes made to the array received not being reflected in the passed array. That was fixed, though.

On Tue, Oct 20, 2009 at 9:49 PM, Seth Tisue <seth@tisue.net> wrote:
>>>>> "Nils" == Nils Kilden-Pedersen <nilskp@gmail.com> writes:

 Nils> Does array.clone work in 2.8?

I don't know if it's a bug or a feature, but in 2.8 toArray copies
arrays:

Welcome to Scala version 2.8.0.r19127-b20091018090027 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_15).

scala> Array(0)
res0: Array[Int] = Array(0)

scala> res0.toArray
res1: Array[Int] = Array(0)

scala> res0(0) = 99

scala> res0
res3: Array[Int] = Array(99)

scala> res1
res4: Array[Int] = Array(0)

--
Seth Tisue @ Northwestern University / http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/



--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.

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