This has worked for me. Modify depending on your case.
CAVEAT: I built a fatjar using $ lein uberjar which I then added as an
external dependency in Eclipse. This included the clojure jar, the
contrib and potentially lots of other stuff I had in my project. It is
not entirely clear to me if one needs to lein compile and to even add
an :aot section to the project.clj file (both of which I did) so some
clarification here would be most welcomed.
clj-code:
---------
(ns me.foo)
;;; A clojure protocol is like a java interface
(defprotocol Animal
(speak [this] "Your usual animal that speaks."))
;;; and a record is sort of like a class (or can be used as one;
generating proper Java classes from clojure is done differently)
;;; so, a Dog is an Animal and speaks!
(defrecord Dog [name breed]
Animal
(speak [this]
(println "Yo! I'm" (:name this) "and I'm a" (:breed this) "dog. Woof!")))
;;; same for Cats ... or so they say.
(defrecord Cat [name breed]
Animal
(speak [this]
(println (:name this) "is my name and I'm a" (:breed this)
"cat. Meow!")))
;;; after compiling and stuff we can try stuff out on the REPL:
me.foo> (def d (Dog. "Rex" "Spaniel"))
#'me.foo/d
me.foo> (speak d)
Yo! I'm Rex and I'm a Spaniel dog. Woof!
nil
me.foo> (def c (Cat. "Michi" "Persian"))
#'me.foo/c
me.foo> (speak c)
Michi is my name and I'm a Persian cat. Meow!
nil
;;; nothing to set the world on fire here really, just standard
clojure stuff however ...
;;; in javaland we can do
package com.me.scratchpad;
import me.foo.Cat;
import me.foo.Dog;
public class CljInterop {
public static void main(String[] args) {
Dog d = new Dog("Rex", "Spaniel");
Cat c = new Cat("Michi", "Persian");
d.speak();
c.speak();
}
}
;;; which when we run, on the console we get:
Yo! I'm Rex and I'm a Spaniel dog. Woof!
Michi is my name and I'm a Persian cat. Meow!
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en