No clear way to access Scala object instances from JRuby
--------------------------------------------------------
Key: JRUBY-6072
URL: https://jira.codehaus.org/browse/JRUBY-6072
Project: JRuby
Issue Type: Bug
Components: Java Integration
Affects Versions: JRuby 1.6.4
Reporter: Arturas Slajus
Priority: Minor
Scala has concept of objects - singleton classes which can be passed around:
{code}
object A {
def str = "Scala string"
}
class B {
def speak(obj: A.type) = println(obj.str)
}
{code}
This can be invoked using:
{noformat}
scala> arturas@arturaz-fujitsu:~/work/scala_jruby$ scala
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) Server VM, Java 1.7.0).
scala> :load a.scala
Loading a.scala...
defined module A
defined class B
scala> val b = new B
b: B = B@1a299f9
scala> b.speak(A)
Scala string
{noformat}
When compiled to JVM bytecode it looks like:
{code:java}
// arturas@arturaz-fujitsu:~/work/scala_jruby$ javap A.class
// Compiled from "a.scala"
public final class A {
public static final java.lang.String str();
}
// arturas@arturaz-fujitsu:~/work/scala_jruby$ javap A\$.class
// Compiled from "a.scala"
public final class A$ implements scala.ScalaObject {
public static final A$ MODULE$;
public static {};
public java.lang.String str();
}
// arturas@arturaz-fujitsu:~/work/scala_jruby$ javap B.class
// Compiled from "a.scala"
public class B implements scala.ScalaObject {
public void speak(A$);
public B();
}
{code}
These objects are used heavily in Scala stdlib. One of them are
Java::scala.None.
The problem is that even if we can access methods on object A (like str()), we
have no clear way of passing it around (like to class instance B, which
actually expects class instance A$).
Also Ruby allows us to pass Java::A to B#speak, however it just fails with no
method, because it actually tries to feed RubyClass to B#speak.
I'm not sure how we should solve this.
One way would be a magic method __scala_object__ added to class X if we can see
that there's X$ and it implements ScalaObject.
Other (perhaps better) way would be detecting if class X has class X$ that
implements ScalaObject and just doing Java::X = Java::X$.new in jruby.
And we definately need to get it documented.
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email