Tags

Java-generics in Scala

April 6th, 2007

Something I overlooked in the tool-docs and complained about:

–Xgenerics
Use generic Java types.

This makes the following possible:

implicit def JavaCollectionToList[T>:Null<:Object] (coll: java.util.Collection[T]) : List[T] = {

coll.iterator();

}

implicit def JavaIteratorToScalaList[T>:Null<:Object](it: java.util.Iterator[T]):List[T]={

it.hasNext match{

case true=> val head = it.next(); head :: JavaIteratorToScalaList(it);
case false=> Nil;

}

}

With these coercions a List[String] can be assigned from a java.util.Collection<String>

Open question: How will this work with .NET generics (not an issue for me at the moment and the IL-code generation seems to have a problem)

Leave a Reply