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

java raw type

2 replies
Ishaaq Chandy
Joined: 2009-02-16,
User offline. Last seen 42 years 45 weeks ago.
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
James Iry
Joined: 2008-08-19,
User offline. Last seen 1 year 23 weeks ago.
Re: java raw type
It's doable in either language, but Scala makes you jump through more hoops when working with Java's raw types.

// 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:
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

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
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>
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

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