Hi, I am playing with GraalVM APIs and realize the...
# announcements
q
Hi, I am playing with GraalVM APIs and realize the polyglot API cannot change Kotlin class members' values, while it can in Java. Why does this happen ??
Copy code
java
import org.graalvm.polyglot.Context;
class Scratch {
  public class Response {
    public int x = 2;
  }
  void run () {
    Context context = Context.create();
    Response r = new Response();
    context.getBindings("js").putMember("r", r);
    context.eval("js", "r.x = 42");
    System.out.println(r.x); // print 42

  }
  public static void main(String[] args) {
    new Scratch().run();
  }
}
g
I’m not exactly sure how Graal works in this case, but most probable because there is no public field in kotlin (Kotlin do not expose fields by default, only properties), instead try to yse
r.setX(42)
q
Oohh thank you, totally forgot about that. adding @JvmField var x: Int = ... makes it work as expected 🙂