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

toArray on collection?

1 reply
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 4 days ago.
Dear all,assuming scala.collection.JavaConversion._ is imported in the current scope,why does the following not compile and produce an error message:
def addSecurities(securities: Collection[_ <: SecurityImpl]) {     addSecurities(securities.toArray[SecurityImpl]);  }
error: missing arguments for method toArray in trait Collection;follow this method with `_' if you want to treat it as a partially applied function addSecurities(securities.toArray[SecurityImpl]);
Thank you very much for your precious support
Best RegardsEdmondo
Lars Hupel
Joined: 2010-06-23,
User offline. Last seen 44 weeks 3 days ago.
Re: toArray on collection?

> def addSecurities(securities: Collection[_ <: SecurityImpl]) {
> addSecurities(securities.toArray[SecurityImpl]);
> }
>
> error: missing arguments for method toArray in trait Collection;
> follow this method with `_' if you want to treat it as a partially applied
> function
> addSecurities(securities.toArray[SecurityImpl]);

The problem is that `java.util.Collection` already has a `toArray` method.

Try one of the following things:

1) "Explicitly" convert `securites` to a Scala `Iterable`:

(securites: Iterable[SecurityImpl]).toArray

2) Import `JavaConverters._`

securites.asScala.toArray[SecurityImpl]

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