- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
toArray on collection?
Sun, 2011-12-11, 16:44
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
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
> 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]