Convert unparameterized Java 1.4 Collection to parameterized Scala Sequence?

First you have to make sure that list has the type java.util. ListSVNDirEntry To do this, use a cast: list. AsInstanceOfjava.util.

ListSVNDirEntry After that, the implicit conversion will be resolved for you if you import the JavaConversions object. An implicit conversion to a Scala sequence exists in the JavaConversions object. See the following example with a list of strings being passed to a method that expects a Scala sequence: scala> val jvlist: java.util.

List_ = new java.util. ArrayListString jvlist: java.util. List_ = scala> jvlist.

AsInstanceOfjava.util. ListString res0: java.util. ListString = scala> import collection.JavaConversions.

_ import collection.JavaConversions. _ scala> def perform(scalaseq: SeqString) = println("ok") perform: (scalaseq: scala.collection. SeqString)Unit scala> perform(res0) ok These conversions do not copy the elements - they simply construct a wrapper around a Java collection.

Both versions point to the same underlying data. Thus, there is no implicit conversion in JavaConversions to immutable Scala lists from mutable Java lists, because that would enable changing the contents of a Scala collection that is guaranteed to be immutable In short - prefer Seq... to List... when defining parameters for methods if you can live with a less specific interface (as in perform above). Or write your own function that does the conversion by copying the elements.

First you have to make sure that list has the type java.util.ListSVNDirEntry. To do this, use a cast: list. AsInstanceOfjava.util.

ListSVNDirEntry After that, the implicit conversion will be resolved for you if you import the JavaConversions object. An implicit conversion to a Scala sequence exists in the JavaConversions object. See the following example with a list of strings being passed to a method that expects a Scala sequence: scala> val jvlist: java.util.

List_ = new java.util. ArrayListString jvlist: java.util. List_ = scala> jvlist.

AsInstanceOfjava.util. ListString res0: java.util. ListString = scala> import collection.JavaConversions.

_ import collection.JavaConversions. _ scala> def perform(scalaseq: SeqString) = println("ok") perform: (scalaseq: scala.collection. SeqString)Unit scala> perform(res0) ok These conversions do not copy the elements - they simply construct a wrapper around a Java collection.

Both versions point to the same underlying data. Thus, there is no implicit conversion in JavaConversions to immutable Scala lists from mutable Java lists, because that would enable changing the contents of a Scala collection that is guaranteed to be immutable. In short - prefer Seq... to List... when defining parameters for methods if you can live with a less specific interface (as in perform above).

Or write your own function that does the conversion by copying the elements.

You have to cast the legacy collection down to the target type. Something along the lines of: perform(list. AsInstanceOfListSVNDirEntry).

Thanks a lot, that helped! – getagrip Oct 20 '10 at 12:58 Only you are casting the Scala collection, not the Java one. – Daniel C.

Sobral Oct 20 '10 at 13:07.

First you have to make sure that list has the type java.util. After that, the implicit conversion will be resolved for you if you import the JavaConversions object. An implicit conversion to a Scala sequence exists in the JavaConversions object.

These conversions do not copy the elements - they simply construct a wrapper around a Java collection. Both versions point to the same underlying data. Thus, there is no implicit conversion in JavaConversions to immutable Scala lists from mutable Java lists, because that would enable changing the contents of a Scala collection that is guaranteed to be immutable.

In short - prefer Seq... to List... when defining parameters for methods if you can live with a less specific interface (as in perform above). Or write your own function that does the conversion by copying the elements.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions