用于调用java工厂方法的clojure中的错误:找到多个匹配方法

时间:2012-09-25 09:57:53

标签: clojure

在这个例子中 - http://jscience.org/api/javax/measure/package-summary.html,当我尝试在clojure中运行时,我收到错误。

(import 'javax.measure.unit.SI)
(import 'javax.measure.Measure)

(Measure/valueOf 2 SI/KILOGRAM)
;; => More than one matching method found: valueOf
[Thrown class java.lang.IllegalArgumentException]

到底有没有?

更新

实际的java代码是这样的:

public static <Q extends Quantity> Measure<java.lang.Integer, Q> valueOf(
        int intValue, Unit<Q> unit) {
    return new Integer<Q>(intValue, unit);
}

public static <Q extends Quantity> Measure<java.lang.Float, Q> valueOf(
        float floatValue, Unit<Q> unit) {
    return new Float<Q>(floatValue, unit);
}

我得到的错误是:

More than one matching method found: valueOf
  [Thrown class java.lang.IllegalArgumentException]

Restarts:
 0: [QUIT] Quit to the SLIME top level

Backtrace:
  0:          Compiler.java:2360 clojure.lang.Compiler.getMatchingParams
  1:          Compiler.java:1555 clojure.lang.Compiler$StaticMethodExpr.
  2:           Compiler.java:938 clojure.lang.Compiler$HostExpr$Parser.parse
  3:          Compiler.java:6455 clojure.lang.Compiler.analyzeSeq
  4:          Compiler.java:6262 clojure.lang.Compiler.analyze
  5:          Compiler.java:6223 clojure.lang.Compiler.analyze
  6:          Compiler.java:5618 clojure.lang.Compiler$BodyExpr$Parser.parse
  7:          Compiler.java:5054 clojure.lang.Compiler$FnMethod.parse
  8:          Compiler.java:3674 clojure.lang.Compiler$FnExpr.parse
  9:          Compiler.java:6453 clojure.lang.Compiler.analyzeSeq

3 个答案:

答案 0 :(得分:2)

很可能您正在调用静态重载方法(即Measure.valueOf(double, Unit) vs Measure.valueOf(float, Unit))。尝试将您的值转换为所需类型(假设您要调用Measure/valueOf(float, Unit)

(Measure/valueOf (float 2) SI/KILOGRAM)

答案 1 :(得分:2)

刚试了一下就可以了:

(Measure/valueOf (Integer. 2) SI/KILOGRAM)

就像这样:

(Measure/valueOf (cast Long 2) SI/KILOGRAM)

答案 2 :(得分:1)

以下是几个基本的演员示例:

(import 'javax.measure.unit.SI)
(import 'javax.measure.Measure)

; Integer Value
; will call: 
;  public static <Q extends Quantity> Measure<java.lang.Integer, Q> valueOf
(Measure/valueOf (Integer. 2) SI/KILOGRAM)

; Long  value 
; will call: 
;  public static <Q extends Quantity> Measure<java.lang.Long, Q> valueOf
(Measure/valueOf (cast Long 2) SI/KILOGRAM)

; Float value
; will call: 
;  public static <Q extends Quantity> Measure<java.lang.Float, Q> valueOf
(Measure/valueOf (Float. 2.0) SI/KILOGRAM)