A solution to arity problem when implementing an interface:

Interface:
(ns Agent.IAgent)

(gen-interface
                                :name "Agent.IAgent"
                                :methods [[senseEnv [int] int]]
)

Implementation:
(ns Agent.AgentIMP
                (:gen-class :name "Agent.AgentIMP" :implements [Agent.IAgent]))

(defn -senseEnv [n]
                        10
)

Java program for testing:
import Agent.*;

public class testAgentIMP{

                public static void main(String[] args){
                                AgentIMP AIMP = new AgentIMP();
                                System.out.println("senseEnv = " + 
AIMP.senseEnv(5));
                }
}

Result:
javac testAgentIMP.java
java testAgentIMP

>>> Exception in thread "main" java.lang.IllegalArgumentException: Wrong number 
>>> of args passed to: AgentIMP$-senseEnv

Explanation: The method "-senseEnv" expects another first argument,
you can use "this":

(ns Agent.AgentIMP
                (:gen-class :name "Agent.AgentIMP" :implements [Agent.IAgent]))

(defn -senseEnv [this n]
                        10
)

javac testAgentIMP.java
java testAgentIMP

>>> senseEnv = 10

The extra argument may not seem obvious from the interface
specification, therefore I post this solution.

But when using the above with clojure 1.2.0 and clojure-contrib 1.2.0,
I get the following error:

Exception in thread "main" java.lang.VerifyError: class Agent.AgentIMP
$loading__4410__auto__ overrides final method meta.()Lclojure/lang/
IPersistentMap;

I have inspected that class and the corresponding interface-class:

user=> (list-declared-methods "Agent.IAgent$loading__4410__auto__")
("public java.lang.Object Agent.IAgent$loading__4410__auto__.invoke()
throws java.lang.Exception" "public clojure.lang.IObj Agent.IAgent
$loading__4410__auto__.withMeta(clojure.lang.IPersistentMap)" "public
clojure.lang.IPersistentMap Agent.IAgent
$loading__4410__auto__.meta()")

user=> (list-declared-methods "Agent.AgentIMP$loading__4410__auto__")
("public java.lang.Object Agent.AgentIMP
$loading__4410__auto__.invoke() throws java.lang.Exception" "public
clojure.lang.IObj Agent.AgentIMP
$loading__4410__auto__.withMeta(clojure.lang.IPersistentMap)" "public
clojure.lang.IPersistentMap Agent.AgentIMP
$loading__4410__auto__.meta()")

I inspected the corresponding classes created with clojure/-contrib
1.1.0, and meta() was not a declared method there, only invoke() was
declared.

So, in clojure/-contrib 1.2.0, why does the class override method
meta() which is final in clojure.lang.Obj (though I was not able to
trace inheritance back to clojure.lang.Obj)?

Regards,
Tommy

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to