3

I'm now making a class object with Clojure which has a method returning the object itself.

Written with Java, the object that I'd like to make is like,

class Point {
    public double x;
    public double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public Point copy() {
        return new Point(this.x, this.y);
    }
}

The current clojure code that I wrote is like,

(ns myclass.Point
  :gen-class
  :prefix "point-"
  :init init
  :state state
  :constructors {[double double] []}
  :methods [[copy [] myclass.Point]]))

(defn point-init [x y]
   [[] {:x x :y y}])

(defn point-copy [this]
   this)

However, I got an error as follows.

java.lang.ClassNotFoundException: myclass.Point

While I have googled about this issue, I couldn't find any answers. Does anyone know the solution for this issue?

Thank you in advance for your help.

4

2 回答 2

5

我不确定这是唯一的方法,但是,为了在方法签名中使用生成的类类型,您可以分两步生成该类 - 尽管它仍然在一个文件中并一次编译:

  • gen-class仅使用构造函数调用
  • 再次调用gen-class状态,全套构造函数和方法。

我尝试了各种方法,包括前向声明,但最终只有上述方法有效。我确实扩展了你的例子。请注意,该方法按原样copy不是很有用,因为它是不可变的,但您可能希望为您的类提供修改器。Point

完整清单:

(ns points.Point)

;; generate a simple class with the constructors used in the copy method
(gen-class
 :name points.Point
 :init init
 :constructors {[] []
                [double double] []})

;; generate the full class 
(gen-class
 :name points.Point
 :prefix pt-
 :main true
 :state coordinates
 :init init
 :constructors {[] []
                [double double] []}
 :methods [[distance [points.Point] double]
           [copy [] points.Point]])

(defn pt-init
  ([] (pt-init 0 0))
  ([x y]
   [[] {:x x :y y}]))

(defn pt-copy 
  "Return a copy of this point"
  [this]
  (points.Point. (:x (.coordinates this)) (:y (.coordinates this))))

(defn pt-distance [^points.Point this ^points.Point p]
  (let [dx (- (:x (.coordinates this)) (:x (.coordinates p)))
        dy (- (:y (.coordinates this)) (:y (.coordinates p)))]
    (Math/sqrt (+ (* dx dx) (* dy dy)))))

(defn pt-toString [this]
  (str "Point: " (.coordinates this)))

;; Testing Java constructors and method call on Point class
(import (points Point))
(defn pt-main []
  (let [o (Point.)
        p (points.Point. 3 4)]
    (println (.toString o))
    (println (.toString p))
    (println (.distance o p))
    (println (.distance p (.copy p)))))

为了生成类,配置project.clj

:aot [points.Point]

测试lein给出:

tgo$ lein clean
tgo$ lein compile
Compiling points.Point
tgo$ lein run
Point: {:x 0, :y 0}
Point: {:x 3.0, :y 4.0}
5.0
0.0
于 2015-03-31T17:48:11.780 回答
0

原因分析

myclass.Point问题是因为编译器在实际生成类之前不知道:methods指令中的内容。myclass.Point虽然这对 Java 类来说不是问题,但 Clojure 编译器似乎不支持这种用例,(也许有充分的理由。)

解决方案

:implements我觉得实现接口(在指令中)比定义自定义方法(如您的示例)要容易得多。这也可能暗示了“为接口编程”的良好实践。从 Clojure 生成接口是可行的,例如gen-interface. 只需确保在表单gen-interface之前编译 aot gen-class

或者,我更喜欢使用 Java 中的接口和 Clojure 中的实现来创建一个多语言项目。这是使用 leiningen 执行此操作的代码片段:

// src/java/points/Point.java
package points;

public interface Point {
    public double distant(Point p);
    public Point copy();
}
;; project.clj
(defproject
  ;; Change these. The rests are the same.
  :aot [points.java-class]
  :source-paths ["src/clojure"]
  :java-source-paths ["src/java"])

;; src/clojure/points/java_class.clj
(ns points.java-class
  (:gen-class
   :name points.Point2D
   :implements [points.Point]  ;; no more ClassNotFoundException
   :prefix "point-"
   :init init
   :state state
   :constructors {[double double] []})

;; rests are the same

这是因为 Leiningen 默认首先编译 java 源代码。

这个答案也包含在我的文章中,尽管我修改了代码片段以适应您的问题。

于 2019-01-31T23:00:44.320 回答