Hi,
On Oct 26, 2:35 pm, Gorsal <[email protected]> wrote:
> Im attempting to create a named class inside another namespace like
> this:
>
> (ns ide.handlers
> (:import [org.eclipse.ui.commands AbstractHandler]))
>
> (defmacro new-handler-class [n event & body]
> `(do
> (ns ~n
> (:gen-class :extends
> ~'org.eclipse.ui.commands.AbstractHandler :init ~'init))
> (defn ~'-init [] [[][]])
> (defn ~'-execute [~'this ~event]
> ~...@body)))
>
> (new-handler-class ide.KeyTest _
> (.print System/out "HI"))
>
> However, when i (compile 'ide.handlers) it generate a class named
> KeyTest$_init_307.class along with KeyTest.class.
> When i attempt to launch, i get the following error
>
> Caused by: java.io.FileNotFoundException: Could not locate ide/
> KeyTest__init.class or ide/KeyTest.clj on classpath:
>
> So, apparently i need a KeyTest_init instead of a KeyTest$_init_307?
> Why in the heck is KeyTest.class not referring to the KeyTest
> $_init_307? How do i get this to work? I don't want to create a new
> file per class name. And renaming the file doesn't work, i guess the
> class name is stored inside somewhere.
My suspicion is, that the problem is the nesting ns in the do. This at
least smells really hard. I would suggest to use simply gen-class
directly and define the functions in the containing namespace with a
suitable prefix.
(defmacro new-handler-class
[classname-sym event & body]
(let [classname (name classname-sym)
last-dot (.lastIndexOf classname ".")
simple-classname (if (not= last-dot -1)
(.substring classname (inc last-dot))
classname)
prefixed #(symbol (str simple-classname "-" %))
hinted-this (with-meta 'this {:tag classname-sym})]
`(do
(gen-class
:name ~classname-sym
:extends org.eclipse.ui.commands.AbstractHandler
:prefix ~(symbol (str simple-classname "-"))
:init ~'init
:main false)
(defn ~(prefixed "init")
[]
[[] []])
(defn ~(prefixed "execute")
[~hinted-this ~event]
~...@body))))
The functions will live in the calling namespace with a suitable
prefix, eg. foo.bar.Baz => Baz-execute.
Not tested but should come close.
Sincerely
Meikel
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---