- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
java raw type
Sat, 2009-02-21, 00:19
Hi all,
I have to implement a Java interface that has a method that takes a raw java.util.List. The library is pre-1.5 so is not generified. Implementations of the method must add strings into the list - yes, it is a rotten design contract, but not my design :)
My understanding is because it is a raw List rather than a List<?> I can't use existential types here. Looking at previous discussions this group has had I understand that the only option I have is to write a Java wrapper class that does the unsafe casts required to get this to work. The discussions I were from mid last year and I was wondering if things have changed since then.
Regards,
Ishaaq
I have to implement a Java interface that has a method that takes a raw java.util.List. The library is pre-1.5 so is not generified. Implementations of the method must add strings into the list - yes, it is a rotten design contract, but not my design :)
My understanding is because it is a raw List rather than a List<?> I can't use existential types here. Looking at previous discussions this group has had I understand that the only option I have is to write a Java wrapper class that does the unsafe casts required to get this to work. The discussions I were from mid last year and I was wondering if things have changed since then.
Regards,
Ishaaq
Sat, 2009-02-21, 01:47
#2
Re: java raw type
Out of interest, is this a library for which source is available? Can you give me something to google?
2009/2/20 Ishaaq Chandy <ishaaq@gmail.com>
2009/2/20 Ishaaq Chandy <ishaaq@gmail.com>
Hi all,
I have to implement a Java interface that has a method that takes a raw java.util.List. The library is pre-1.5 so is not generified. Implementations of the method must add strings into the list - yes, it is a rotten design contract, but not my design :)
My understanding is because it is a raw List rather than a List<?> I can't use existential types here. Looking at previous discussions this group has had I understand that the only option I have is to write a Java wrapper class that does the unsafe casts required to get this to work. The discussions I were from mid last year and I was wondering if things have changed since then.
Regards,
Ishaaq
// the Java interface
import java.util.List;
public interface JInterface {
public void doIt(List in);
}
// the Scala implementation
import java.util.List
class Test extends JInterface {
// use an existential in the interface
override def doIt(rawList : List[_] ) {
// this isn't legal
// rawList add "hello"
// but if cast it's ok
val list = rawList.asInstanceOf[List[String]]
// we're good to go
list add "hello"
}
}
On Fri, Feb 20, 2009 at 3:14 PM, Ishaaq Chandy <ishaaq@gmail.com> wrote: