On Thu, Apr 8, 2010 at 7:46 PM, Yoko H. [email protected] wrote:
It’s easy to stop Integer’s auto coercion. Just commenting out the
line below in org.jruby.javasupport.JavaUtil,
JAVA_CONVERTERS.put(Integer.class, JAVA_INT_CONVERTER);
then Integer is proxied like other user defined classes.
Yeah, it’s not a complicated change to make, but as you point out
below it would be a behavioral change.
However, I think this easy fix has at least two problems. The all
Integer values will be always Java::JavaLang::Integer type and don’t
have any way to convert to ruby type. Maybe, to_i or to_ruby should be
supported? And the bigger problem is… for users who expect auto
coercion, the change will be regression. We may need a new option to
stop auto coercion or something that controls turning on/off coercion.
I have secretly wanted to turn off all auto-coercion of Java values
entering Ruby simply because they automatically make it impossible to
get at the original Java objects (for example, it’s really hard to get
access to java.lang.String.charAt or other string methods). In some
cases, they can actually be a perf issue (like Strings having to be
UTF-8 encoded/decoded on the way in and out). But I haven’t been able
to reconcile the fact that people expect these coercions now with my
desire to eliminate them. We’re a bit stuck.
In a future “JRuby 2.0” it seems like the natural progression would be
to remove these auto-coercions and make the auto-coercing Java types
duck-type to the appropriate Ruby types. So for example:
- java.lang.CharSequence/String would implement most Ruby String
methods but act like a frozen Ruby String (no mutation)
- java.lang.Number subclasses would implement methods from Numeric and
appropriate subclasses Integer, Float, Fixnum, etc.
The ultimate goal would be to make those types act like the Ruby
equivalents and coerce to the Ruby equivalents (with to_s, to_str,
to_i, to_int, etc), but not actually coerce until you tell them to.
We could probably start this process by adding methods to the Java
types, similar to the work you did on making Java Maps look like a
Ruby Hash or the work I just did adding Ruby Exception methods to
java.lang.Throwable. Once we get to a point where we have all the
duck-typing in place, we could provide a flag or option to turn off
auto-coercing, and people could start to play with it. That would show
us where the gaps are. (and perhaps we could provide the flag earlier,
for people who are comfortable with the idea that Java values would
stop auto-coercing?)
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email