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

List to String

4 replies
kolotyluk
Joined: 2010-06-04,
User offline. Last seen 5 weeks 15 hours ago.

Is there any easy way in Scala to convert a List to an Array?

Is seems easy enough to convert an Array to a List, so why is the
converse so difficult?

For example, if I have a List[String] how do I convert it to and
Array[String]?

Cheers, Eric

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: List to String
It's just .toArray.

You may have found that if you're using generic types that this doesn't work so well.  The reason is that the JVM handles arrays differently from everything else (in that arrays' types are _not_ erased).  So there's a problem: if the code is fully generic, it has no idea what type it is, yet the array demands to know the type.

Fortunately, there's an easy solution: add : Manifest after the type.  For example:
  def example[A: Manifest](xs: List[A]) = xs.toArray

  --Rex

On Sun, Jan 29, 2012 at 11:48 PM, Eric Kolotyluk <eric.kolotyluk@gmail.com> wrote:
Is there any easy way in Scala to convert a List to an Array?

Is seems easy enough to convert an Array to a List, so why is the converse so difficult?

For example, if I have a List[String] how do I convert it to and Array[String]?

Cheers, Eric

kolotyluk
Joined: 2010-06-04,
User offline. Last seen 5 weeks 15 hours ago.
Re: List to String
No - it is not just .toArray as that gives me an error. My code looks like

     case command :: tail => perform(command, tail)

 . . .

    def perform(command : String, arguments : Array[String]) = {


and the compiler knows that tail is List[String] - so why can't the compiler just do what is obvious?

On 2012-01-29 8:53 PM, Rex Kerr wrote:
QYW_C0EwD3zHAg [at] mail [dot] gmail [dot] com" type="cite">It's just .toArray.

You may have found that if you're using generic types that this doesn't work so well.  The reason is that the JVM handles arrays differently from everything else (in that arrays' types are _not_ erased).  So there's a problem: if the code is fully generic, it has no idea what type it is, yet the array demands to know the type.

Fortunately, there's an easy solution: add : Manifest after the type.  For example:
  def example[A: Manifest](xs: List[A]) = xs.toArray

I don't know what you mean. Can you give a code example in context please.

Cheers, Eric

QYW_C0EwD3zHAg [at] mail [dot] gmail [dot] com" type="cite">
  --Rex

On Sun, Jan 29, 2012 at 11:48 PM, Eric Kolotyluk <eric [dot] kolotyluk [at] gmail [dot] com" rel="nofollow">eric.kolotyluk@gmail.com> wrote:
Is there any easy way in Scala to convert a List to an Array?

Is seems easy enough to convert an Array to a List, so why is the converse so difficult?

For example, if I have a List[String] how do I convert it to and Array[String]?

Cheers, Eric

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: List to String


On Mon, Jan 30, 2012 at 12:01 AM, Eric Kolotyluk <eric.kolotyluk@gmail.com> wrote:
No - it is not just .toArray as that gives me an error. My code looks like

     case command :: tail => perform(command, tail)

 . . .

    def perform(command : String, arguments : Array[String]) = {


and the compiler knows that tail is List[String] - so why can't the compiler just do what is obvious?

Because you haven't asked it to, and it's an expensive operation.  You can't do it the other way, either:
  def perform(cmd: String, args: List[String]) = {...}
does not work if you pass it an Array[String].

Just .toArray it and you'll be fine.  (.toList when you want to go the other way.)

Maybe you're confused because Array _can_ be converted to a Seq by means of an inexpensive wrapper (and this is done by an implicit conversion).  But a Seq is not a list, and going the other way (Seq to Array) may require an expensive copy of every item in the Seq, so you have to do that one by hand.

  --Rex

kolotyluk
Joined: 2010-06-04,
User offline. Last seen 5 weeks 15 hours ago.
Re: List to String
OK, I got it working with

  case command :: tail => perform(command, tail.toArray)

Which was what I had in the first place, but Eclipse was flagging it as an error. Now Eclipse is happy with it for some reason.

I think I was confused because Eclipse was playing games with me.

Cheers Eric


On 2012-01-29 9:06 PM, Rex Kerr wrote:
5Um_1ctEpFx_HxJqjmPGHQoPFtjdw1tJN1bBcKWhXA [at] mail [dot] gmail [dot] com" type="cite">

On Mon, Jan 30, 2012 at 12:01 AM, Eric Kolotyluk <eric [dot] kolotyluk [at] gmail [dot] com" rel="nofollow">eric.kolotyluk@gmail.com> wrote:
No - it is not just .toArray as that gives me an error. My code looks like

     case command :: tail => perform(command, tail)

 . . .

    def perform(command : String, arguments : Array[String]) = {


and the compiler knows that tail is List[String] - so why can't the compiler just do what is obvious?

Because you haven't asked it to, and it's an expensive operation.  You can't do it the other way, either:
  def perform(cmd: String, args: List[String]) = {...}
does not work if you pass it an Array[String].

Just .toArray it and you'll be fine.  (.toList when you want to go the other way.)

Maybe you're confused because Array _can_ be converted to a Seq by means of an inexpensive wrapper (and this is done by an implicit conversion).  But a Seq is not a list, and going the other way (Seq to Array) may require an expensive copy of every item in the Seq, so you have to do that one by hand.

  --Rex

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