Re: [tcljava-user] Returning generics to TclBlend
Brought to you by:
mdejong
From: Mo D. <mo...@mo...> - 2007-04-09 21:23:10
|
Tanzer, Troy wrote: > I'm not sure if this belongs here or on the developer list, but here is > my problem. We have a library of functions written in Java, and access > the libraries using TclBlend. When those functions return a specific > class of Java object, we can use them in the tcl scripts and call > methods on them and all works fine. However, when those functions > return generics (we are using Java 1.5), the TclBlend shell is confused. > I can access the type of the object, but TclBlend sees it as only > java.lang.Object, and only allows Object methods on it until I cast it. > > So, I can access the class type on the Tcl side, and work around this on > the Tcl side by casting to the correct object type. Is there anything I > could do on the Java side (outside of not using generics) that would > eliminate the need to cast the return to the correct type on the Tcl > side? > Hi troy My understanding of how Java implements generics is that the compiler creates compiled code that does the upcast behind the scenes. It may be possible to implement this type of functionality in TclJava, but it is not implemented now. So, doing the upcast is needed currently. You may be able to work around this issue by creating a util Java method that returns the objects you create already upcasted to the specific type. For example, if a container contained String objects, you could create a method that would have the String return type. This method would get an element out of a container and cast it to String in Java code. You would then invoke this method from Tcl code and your Tcl code would not need to know anything about generics. You might also want to look into a Tcl util method that would examine the contents of a container and do the upcast action via java::cast for you. Another option would be to modify Jacl itself, that might take a bit longer but would be better for everyone in the long run, assuming you were interested in making the changes and donating them to the project. For example, you might want to take a look at the new java::for command that I added to 1.4.0, it supports iteration over objects that support the generics API. cheers Mo DeJong |