jython type coercion to interface type

Ype Kingma ykingma at accessforall.nl
Thu Jun 26 06:17:52 CEST 2003


Harald Kirsch wrote:

> How would I call a java constructor which takes a java.lang.CharSequence
> as argument. Say, the java class goes like this
>
> class X {
>   public X(CharSequence s) { ... }
> }
>
> In the jython interpreter I tried along the lines of
>
>   x = X("bla")
>
> and got "1st arg can't be coerced to java.lang.CharSequence". Then

This is a known bug in jython 2.1, see
http://aspn.activestate.com/ASPN/Mail/Message/Jython-dev/1582382

>   x = X(CharSequence("bla"))
>
> and got "can't instantiate interface (java.lang.CharSequence)"
>
> Is there a way to typecast String to CharSequence or, mor generally,
> a type to a supertype or interface?

Could you try this:

from java.lang import String

x = X(String("bla"))

and report whether it's a workaround for the bug?
For Java types, such conversions normally succeed in Jython.
The problem seems to be that the standard string conversion
from jython to java is incomplete.

Kind regards,
Ype

--
email at xs4all.nl




More information about the Python-list mailing list