clojure-dev

Issues: https://clojure.atlassian.net/browse/CLJ | Guide: https://insideclojure.org/2015/05/01/contributing-clojure/
2019-09-02T08:14:46.027300Z

Is there a way to take a deftype and use its class in Java source code?

2019-09-02T08:15:40.028Z

I know I could create Java source code for a similar class with the same attributes, fields, etc., but that would not be the same class, I don't think.

cgrand 2019-09-02T08:20:34.029800Z

if the deftype is an AOT lib it should just work, if it’s in the same project the build will be unusual (and IDEs may be confused).

2019-09-02T08:20:48.030200Z

and the only reason I have for asking is to write as high performance Java as possible accessing instances of such a class, so reflection API is not a good contender for this purpose.

2019-09-02T08:21:51.030800Z

Hmmm. It might be an AOT lib, given that the deftype I have in mind is the Vec type in Clojure itself.

2019-09-02T08:23:51.033Z

How would you compile a Java source file that accesses the class that is defined via the deftype? I was thinking that it would need a Java source version of the class definition, but are you saying that a .class file produced via AOT on Clojure code is enough?

2019-09-02T08:25:19.033500Z

I will go hack up a quick experimental Java source file with this to see if I can get it working. Thanks.

cgrand 2019-09-02T08:44:54.034300Z

$ cat src/Main.java
class Main {
  public void main() {
    new clojure.core.Vec(); // pass proper args for compilation to succeed
  }
}
$ javac -cp ~/.m2/repository/org/clojure//clojure/1.10.0/clojure-1.10.0.jar src/Main.java
src/Main.java:3: error: constructor Vec in class Vec cannot be applied to given types;
    new clojure.core.Vec();
    ^
  required: Object,int,int,Object,Object,Object
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

2019-09-02T08:45:57.034800Z

Thanks. I was distracted by something else for a while, and my experiment had not started yet. Looks good.

2019-09-02T17:23:42.035200Z

yep, I can get a full call working that way. Thanks.

cfleming 2019-09-02T23:48:27.036700Z

@andy.fingerhut I do this in Cursive. One thing to be aware of is that loading the class will not by default load its containing namespace, so if you call other functions from that namespace they will fail unless you use the :load-ns true option to deftype.